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

[CLOSED] For loop stopping for no apparent reason?

below is a script with zero errors:

players = game.Players:GetPlayers()
while true do
	wait(1)	
	print("test")
	for i,v in pairs(players) do
		print("test2")
		local character = v.Character or v.CharacterAdded:Wait()
		repeat
			wait()
		until character
		local humanoid = character:WaitForChild("Humanoid")
		if character:FindFirstChild("thirst").Value <= 20 then 	
			humanoid.Health = humanoid.Health - 40	
		end
		if character:FindFirstChild("hunger").Value <= 20 then 	
			humanoid.WalkSpeed = 5	
		else	
			humanoid.WalkSpeed = 12	
		end
		tick()
	end
end

no errors? then what is the problem? you may ask, I have no clue... where does it stop? it stops at the part where it says:

for i,v in pairs(players) do

it prints "test" but not "test2"

EDIT: i found out to fix it i had to put :getplayers into the while loop and make it local

2 comments

0

Maybe try doing players:GetPlayers() in the pairs loop, new to roblox as well...

0

for i,v in pairs(players) do needs players so if i defined it after that it would not worl

1 answer

✓ Accepted answer

Try:

local Players = game:GetService("Players")

local function ThirstHunger(Player)
	while true do
		print("Test2")
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Humanoid = Character:WaitForChild("Humanoid")
		if Character.thirst.Value <= 20 then
			Humanoid.Health = Humanoid.Health - 40
		end
		if Character.hunger.Value <= 20 then  
            Humanoid.WalkSpeed = 5  
        else    
            Humanoid.WalkSpeed = 12 
		        end
		wait(1)
	end
end


Players.PlayerAdded:Connect(ThirstHunger)

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