local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local workspace = game:GetService("Workspace")
local pause = false
local localPlayer = players.LocalPlayer
local renderCache = replicatedStorage:WaitForChild("RenderCache")
local character
local humanoidRootPart
local render = workspace:WaitForChild("ObjRender")
local parts = {}
local renderDistance = 200
function Scan()
parts = {} -- Clear the parts table
for i, obj in next, render:GetChildren() do
table.insert(parts, obj)
obj.Parent = renderCache
end
end
function GetPart(obj)
if obj:IsA("BasePart") then
return obj
else
for i, obj in next, obj:GetChildren() do
return GetPart(obj)
end
end
return nil
end
function Fix(m)
for _, i in pairs(m:GetChildren()) do
if i:IsA("BasePart") then
i.Parent = render
else
Fix(i)
end
end
end
function Update()
if not pause then
renderDistance = workspace:GetRealPhysicsFPS() * 2
for e, v in next, parts do
local part = GetPart(v)
if part then
local distance = (part.CFrame.p - humanoidRootPart.CFrame.p).Magnitude
distance = math.floor(distance + 0.5)
if distance <= renderDistance then
v.Parent = render
else
v.Parent = renderCache
end
end
end
end
end
localPlayer.CharacterAdded:Connect(function(char)
pause = false
character = char
humanoidRootPart = character:WaitForChild("HumanoidRootPart")
character:WaitForChild("Humanoid").Died:Connect(function()
pause = true
end)
-- Clear the render cache and re-scan parts when the player respawns
for _, v in next, renderCache:GetChildren() do
v:Destroy()
end
Scan()
Update() -- Call the Update function to ensure parts are updated
end)
Fix(render)
Scan()
runService:BindToRenderStep("RenderSys", 1, Update)