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)

6 comments
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.
Can you help me?
He just did
The thing is that I don't know how to break the loop once the players leaves.
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.
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.