Will this code be performant enough in high player servers?

Sorry if this is a dumb question, I feel confident in scripting but unconfident more in performance impacts of using different methods. Basically in my game I need to track how many studs a player has climbed in realtime while they are climbing. Currently, the code I have doing that works a treat. But since I’m using “while task.wait() do” for each player would this become an issue performance wise if this is running simultaneously on the server for 18 players (the amount I plan on allowing in a server)?

this is a snippet of the while loop:

	while task.wait() do
		local character = player.Character
		


		if not character then continue end

		if not lastDistance then lastDistance = character.PrimaryPart.Position end

		if character["Humanoid"]:GetState() ~= Enum.HumanoidStateType.Freefall and character["Humanoid"]:GetState() ~= Enum.HumanoidStateType.Jumping and character["Humanoid"]:GetState() == Enum.HumanoidStateType.Climbing and lastDistance ~= nil then
			local distanceChange = (character.PrimaryPart.Position.Y - lastDistance.Y)
			
			lastDistance = character.PrimaryPart.Position
			
			if distanceChange <= 0 then continue end
			
			studsTravelled.Value += distanceChange
		elseif character["Humanoid"]:GetState() == Enum.HumanoidStateType.Freefall or character["Humanoid"]:GetState() == Enum.HumanoidStateType.Jumping or character["Humanoid"].Jump then
			print("studs cant be counted")
		else
			lastDistance = character.PrimaryPart.Position
		end
	end

Appreciate any help, would it perhaps be better if I did the calculations on the client and fired a remote event to the server or will this do?

Many thanks,
Pyxrien.

Well there are ways you can improve it, first of all, you shouldn’t check the state every second, rather than doing that I suggest checking it every time the state of the humanoid changes.

I know you want it to change simultaneously to do that you can assign it to a variable and check that variable instead of checking the player’s state again and again.

I might make a few mistakes in the code but I think you’ll get the idea.

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local check = false

humanoid.StateChanged:Connect(function(oldState, newState)
      if not lastDistance then lastDistance = character.PrimaryPart.Position end
     
     if newState ~= Enum.HumanoidStateType.Freefall and newState ~= Enum.HumanoidStateType.Jumping and newState == Enum.HumanoidStateType.Climbing and lastDistance ~= nil then
        check = true
    elseif newState == Enum.HumanoidStateType.Freefall or character["Humanoid"]:GetState() == Enum.HumanoidStateType.Jumping or character["Humanoid"].Jump then
        check = false
     else 
        lastDistance = character.PrimaryPart.Position
     end
end

while (check) do
     task.wait()

     local distanceChange = (character.PrimaryPart.Position.Y - lastDistance.Y)
			
     lastDistance = character.PrimaryPart.Position
			
     if distanceChange <= 0 then continue end
			
     studsTravelled.Value += distanceChange
end
1 Like
while task.wait() do
	local character = player.Character
	if not character then continue end
	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid then continue end
	local root = character.PrimaryPart
	if not root then continue end
	lastDistance = lastDistance or root.Position
	if humanoid:GetState().Name ~= "Climbing" then continue end
	studsTravelled.Value += math.max(0, (root.Position.Y - lastDistance.Y))
	lastDistance = root.Position
end