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

[SOLVED] my thirst decreasing script doesn't work, help?

Asked by
8_zn 21
5 years ago
Edited 5 years ago

I made a server script and place it into ServerScriptService. The script is suppose to decrease their thirst and when I tested it I received no errors from the script and the script doesn't work at all. Can someone help me out here please? If you can then thank you!

Script:

print("Loading Thirst..")
wait(1)
print("Thirst successfully loaded!")

--//Services
local Players = game:GetService("Players")

--//Function
Players.PlayerAdded:Connect(function(Player)

    --//Variables
    local MaxThirst = Player.Stats:WaitForChild("MaxThirst")
    local Thirst = Player.Stats:WaitForChild("Thirst")
    local Humanoid = Player.Character:WaitForChild("Humanoid")
    local DecreaseRate = 1
    local SubtractThirst = 2
    local SubractHealth = 100

    --//Main
    while wait(DecreaseRate) do
        if Thirst.Value - 1 >= 0 then
            Thirst.Value = Thirst.Value - SubtractThirst
        end
        if Thirst.Value == 0 then
            repeat wait(2.5)
                Humanoid.Health = Humanoid.Health - SubractHealth
            until Thirst.Value > 0 or Humanoid.Health <= 0
        end
        if Humanoid.Health == 0 then
            Player.Stats.MaxThirst:Destroy()
            Player.Stats.Thirst:Destroy()
        end
    end
end)
0
wait so you want to make a new infinite loop everytime a player is added? and you have nothing to handle when the player leaves either. SteamG00B 1633 — 5y
0
Can you help me? 8_zn 21 — 5y
0
He just did DinozCreates 1070 — 5y
0
The thing is that I don't know how to break the loop once the players leaves. 8_zn 21 — 5y
View all comments (2 more)
0
Do a while loop for the repeat, but make a bool variable to turn it off. For example: while playerHere == true do, and when you want to turn it off just make the variable false. Knineteen19 307 — 5y
0
But my question is, wouldn't it already stop the loop when someone leaves? It would remove the Thirst value along with the player, causing errors in the script, and stopping the loop. Knineteen19 307 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

Not entirely sure if this is the issue, but I think it's because you put:

if Thirst.Value == 0 then
    repeat wait(2.5)
        Humanoid.Health = Humanoid.Health - SubractHealth
    until Thirst.Value > 0 or Humanoid.Health <= 0
end

I don't know too much about the whole repeat thing, but this is my best guess as to what's wrong. Instead of using repeat wait(2.5) or whatever, try this:

if Thirst.Value == 0 then
    while wait(2.5) do
        if humanoid.Health == 0 then
            break
        end
        humanoid.Health = humanoid.Health - SubtrackHealth
    end
end

Not sure if I did much for you, but this is the best I got, I hope it works!

1
When you commented about the breaking the loop it fixed it. Thank you! 8_zn 21 — 5y
1
Oh, well I'm glad I helped! Knineteen19 307 — 5y
Ad

Answer this question