Hello.
So i had to make a Custom render distance script for my very lagy game.
NO, There no viruses in it. It is just that the game is very recourse intensive.
So my problem is: The script breaks when the player dies. I have made two different versions with the same issue, But they have a little bit different outcome.
Can you help me? I created a "Pause" system to fix this. Also this is the script for the "Pause system."
--This pause system is from version 2 from the render distance script.
local pause = false
local runService = game:GetService("RunService")
function Update()
if not pause then
--Do render distancing stuff
end
end
localPlayer.CharacterAdded:Connect(function(character)
pause = false
character = localPlayer.Character
character:WaitForChild("Humanoid").Died:Connect(function()
pause = true
end)
end)
runService:BindToRenderStep("RenderSys", 1, Update)
This just helps a little. Now the whole game does not dissapear when you respawn, But the script does not work anymore.
Here is version 1 of the script: (Must be localscript in startercharacterscripts)
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 = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local render = Workspace:WaitForChild("Render")
local parts = {}
local renderDistance = 200
function Scan()
for _, 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 _, 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 _, 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(character)
pause = false
character = script.Parent
character:WaitForChild("Humanoid").Died:Connect(function()
pause = true
end)
end)
Fix(render)
Scan()
runService:BindToRenderStep("RenderSys", 1, Update)
And version 2: (Must be localscript in starterplayerscripts)
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
repeat
wait()
character = localPlayer.Character
until character
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local render = Workspace:WaitForChild("Render")
local parts = {}
local renderDistance = 200
function Scan()
for _, 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 _, 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 _, 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(character)
pause = false
character = localPlayer.Character
character:WaitForChild("Humanoid").Died:Connect(function()
pause = true
end)
end)
Fix(render)
Scan()
runService:BindToRenderStep("RenderSys", 1, Update)
Can you please fix version 1? It is the easiest way to do it. Note: Also i like version 1 better because its the "original script". I modified version 2.

3 comments
will try
"Infinite yield possible on 'Players.hasanchik.PlayerScripts:WaitForChild("HumanoidRootPart")'" you mean put version 1 in startercharacterscripts
ok fixed "Players.hasanchik.PlayerScripts:WaitForChild("HumanoidRootPart")" but now there is still a problem when you die the script does not work anymore the things you loaded in stay loaded in