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

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

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

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.

0

Can you help me?

0

He just did

0

The thing is that I don't know how to break the loop once the players leaves.

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.

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.

1 answer

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!

2 comments

1

When you commented about the breaking the loop it fixed it. Thank you!

1

Oh, well I'm glad I helped!

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