Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Render distance script breaks when player dies?

Asked by 12 days ago

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.

1 answer

Log in to vote
0
Answered by 11 days ago
Edited 11 days ago

i would highly recommend putting version 1 in the StarterPlayerScripts as it looks designed to continue running. but the main issue here is on line 73 in version 1. I understand that you're trying to set the global variable "character", but the script sees it as setting the local variable "character" (line 71).

an easy fix would be this (71-73):

localPlayer.CharacterAdded:Connect(function(char)
    pause = false
    character = char

it's as easy as setting the name of the local variable "character" to "char" (so the script doesn't confuse the two).

it's never a good idea to have variables of the same name in a script. both you and the script will get confused.

0
will try hasanchik 33 — 10d
0
"Infinite yield possible on 'Players.hasanchik.PlayerScripts:WaitForChild("HumanoidRootPart")'" you mean put version 1 in startercharacterscripts hasanchik 33 — 10d
0
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 hasanchik 33 — 10d
Ad

Answer this question