Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Game script timeout Error - While true do - How to fix?

I am trying to make my script repeat so I am using While true do however whenever I use it it does Game script timeout. I think I need wait somewhere but I've put it everywhere I can think of. Here's the code:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
			while true do
				
				if player.leaderstats.speed.Value <= 16 then
					humanoid.WalkSpeed = 16 
				else
					humanoid.WalkSpeed = 0.1 * player.leaderstats.speed.Value
				wait(1)
			end
		end
    end)
end)

2 comments

0

try replacing true with wait()

0

I have no idea how that did it but thanks!

1 answer

✓ Accepted answer

Your wait(1) function is inside the else statement, meaning that if the original condition is met(player.leaderstats.speed.Value <= 16) then the program won't wait.

fix:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        local humanoid = char:WaitForChild("Humanoid")
            while true do

                if player.leaderstats.speed.Value <= 16 then
                    humanoid.WalkSpeed = 16
wait(1) 
                else
                    humanoid.WalkSpeed = 0.1 * player.leaderstats.speed.Value
                wait(1)
            end
        end
    end)
end)

2 comments

0

Thanks, I understand now

0

np

This discussion is preserved as it was. New answers, comments, and votes are closed.