Im trying to make an anti lag system, by making the trees disappear when not in range and appear when in range,
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
game:GetService("RunService").RenderStepped:Connect(function()
for i, v in pairs(workspace.Trees:GetChildren()) do
if v:FindFirstChild("Part") and v:FindFirstChild("Trunk") then
if v:IsA("Model") then
if not workspace.Trees:FindFirstChild("Tree"..i) then
local clone = v:Clone()
clone.Name = "Tree"..1
local distance = (char:WaitForChild("HumanoidRootPart").Position - v.Trunk.Position).Magnitude
if distance < 20 then
clone.Parent = workspace.Trees
else
clone:Destroy()
v.Parent = game.ReplicatedStorage.ThingsThatCantSee
end
end
end
end
end
end)
i tried this and i know it wont work, but i still cant figure out a way to do it
help
Running a script every frame will probably induce more lag than remove. Try setting the trees’ render fidelity to “performance”. This lets Roblox handle optimizations much better than scripts can.
RenderStepped is once per frame (60 times per second if on a fast computer). Specifically just before the scene is rendered.
RunService also houses events that allow your code to adhere to Roblox’s frame-by-frame loop, such as Stepped, Heartbeat and RenderStepped. Selecting the proper event to use for any case is important, so you should read Task Scheduler to make an informed decision.
They also point out that this event isn’t parallelized and should only be used for small camera movement scripts. Otherwise it could induce serious lag.
RenderStepped does not run in parallel to Roblox’s rendering tasks and code connected to RenderStepped must be executed prior to the frame being rendered. This can lead to significant performance issues if RenderStepped is used inappropriately. To avoid this, only use RenderStepped for code that works with the camera or character. Otherwise, RunService.Heartbeat should be used.