This simple script lags out my entire server underload

Im trying to update leader stats every second and change a percentage based on there Y value.

I cant really provide videos, but basically when the server is full with 20 people it freezes and causes other server timers to freeze.

The only solution I have done is disable it LOL. I cant think of anything else, I did change it from a string value to Int.

local Players = game:GetService("Players")
local Index = 0
local SpawnY = 4
local LastStageY = 2468

while(wait(1)) do
	local allPlayers = Players:GetPlayers()

	for k, player in ipairs(allPlayers) do
		if not(player:FindFirstChild("leaderstats")) then continue end
		local leaderstats = player.leaderstats
		local progressStat = leaderstats and leaderstats:FindFirstChild("Progress")
		if progressStat then
			progressStat.Value = ((player.Character:WaitForChild("HumanoidRootPart").Position.Y - SpawnY) * 100) / (LastStageY - SpawnY)

The leaderstats instance is created on a ServerScriptService file. Tied to a player added event.

local leaderstats ="Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local percentThrewRun ="IntValue")
	percentThrewRun.Name = "% Done"
	percentThrewRun.Value = 0
	percentThrewRun.Parent = leaderstats

Any help and advice is appreciated. My guess is maybe to many operations are happening for it to handle in one second on one thread. Or maybe waiting for HumanoidRootPart is freezing up the server.

It could be the WaitForChild, (not recommended in loops in general, or on the server, as if the player has left during the loop it will hang here)
Try changing to FindFirstChild with an if check then get the position.

1 Like

lol, yeah I just realized that and added it to the my edit. Ill try changing it to :FindFirstChild() and then check to see if its nil. Ill see if that works under load. Thanks.

1 Like

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