Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[CLOSED] For loop stopping for no apparent reason?

Asked by
TickoGrey 116
4 years ago
Edited 4 years ago

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

0
Maybe try doing players:GetPlayers() in the pairs loop, new to roblox as well... PoWerofThEePg 43 — 4y
0
for i,v in pairs(players) do needs players so if i defined it after that it would not worl TickoGrey 116 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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

Answer this question