Anti Lag for low devices

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()

	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
						v.Parent = game.ReplicatedStorage.ThingsThatCantSee

i tried this and i know it wont work, but i still cant figure out a way to do it

try replacing pairs with ipairs

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.

1 Like

RenderStepped is 60 times a frame I think lol

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.

quote RunService docs

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.


1 Like

I tried it in mobile, But its still laggy, thats why I want the trees to ONLY show when its near the players, any chance i could really do that

You don’t have to make a script that unloads objects in certain range since Content Streaming can do it for you.


Omg, i didnt think of that lol. I dont usually go to workspace’s properties but this a big help. Thank you!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.