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:

01players = game.Players:GetPlayers()
02while true do
03    wait(1)
04    print("test")
05    for i,v in pairs(players) do
06        print("test2")
07        local character = v.Character or v.CharacterAdded:Wait()
08        repeat
09            wait()
10        until character
11        local humanoid = character:WaitForChild("Humanoid")
12        if character:FindFirstChild("thirst").Value <= 20 then 
13            humanoid.Health = humanoid.Health - 40 
14        end
15        if character:FindFirstChild("hunger").Value <= 20 then 
View all 22 lines...

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:

1for 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:

01local Players = game:GetService("Players")
02 
03local function ThirstHunger(Player)
04    while true do
05        print("Test2")
06        local Character = Player.Character or Player.CharacterAdded:Wait()
07        local Humanoid = Character:WaitForChild("Humanoid")
08        if Character.thirst.Value <= 20 then
09            Humanoid.Health = Humanoid.Health - 40
10        end
11        if Character.hunger.Value <= 20 then 
12            Humanoid.WalkSpeed = 5 
13        else   
14            Humanoid.WalkSpeed = 12
15                end
View all 21 lines...
Ad

Answer this question