Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

GUI's break so I add a part rendering script to reduce lag might fix it but it won't work, why?

Asked by 4 years ago

All GUI's break randomly when testing or playing my game. I tried fixing it by putting a local script in StarterPlayer in StarterPlayerScripts, the local script is below. After that, i put a CFrameValue and renamed it Renderable inside parts but that does not work, i don't know anymore solutions. But, the script won't work! Idk why so someone please help me, thank you.

local HumanoidRootPart
local Workspace = game.Workspace
local Player = game.Players.LocalPlayer

local RenderParts = {}

local MaxRenderRadius = 50
local MinRenderOutRadius = MaxRenderRadius * 1

for i,v in pairs(game.Workspace:GetChildren()) do
    if v:FindFirstChild("Renderable") then
        table.insert(RenderParts, v)
    end
end

local function CharacterAdded(Character)
    if Character then
        HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
    end
end

CharacterAdded(Player.Character)
Player.CharacterAdded:Connect(CharacterAdded)

local function SetPartVisible(PartName, Visible)
    if Visible == false then
        PartName.Parent = nil
    end
    if Visible == true then
        PartName.Parent = Workspace
    end
end

while true do
    wait(0.1)
    if HumanoidRootPart then
        for i=1, #RenderParts do

            if RenderParts[i].ClassName ~= "Model" then
                if (HumanoidRootPart.Position - RenderParts[i].Position).Magnitide >= MinRenderOutRadius then
                    SetPartVisible(RenderParts[i], false)
                end
            end
            if RenderParts[i].ClassName == "Model" then
                if (HumanoidRootPart.Position - RenderParts[i]:GetPrimaryPartCFrame().p).Magnitide >= MinRenderOutRadius then
                    SetPartVisible(RenderParts[i], false)
                end
            end
            if RenderParts[i].ClassName ~= "Model" then
                if (HumanoidRootPart.Position - RenderParts[i].Position).Magnitide <= MaxRenderRadius then
                    SetPartVisible(RenderParts[i], true)
                end
            end
            if RenderParts[i].ClassName == "Model" then
                if (HumanoidRootPart.Position - RenderParts[i]:GetPrimaryPartCFrame().p).Magnitide >= MaxRenderRadius then
                    SetPartVisible(RenderParts[i], true)
                end
            end

        end
    end
end

Answer this question