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

Stat script not functioning properly?

**Before you say something, YES I have created the value/stat model already.

Output window is totally clean so I don't see any reason why this shouldn't work.

game.Players.PlayerAdded:connect(function(player)
	
    local Thirst = player.stats.Thirst
	
	while Thirst.Value >= 0 do
	Thirst.Value = Thirst.Value -1
	wait(1)
	if Thirst.Value == 0 then
	player.Character.Humanoid.Health = player.Character.Humanoid.Health -5
	wait(10)
	end
	


	


	end
end)

2 comments

0

put some prints around the script

0

SO. for some reason taking out my wait time fixed it but I don't understand why I didn't work without the wait time before... (you don't see the wait time in this post)

1 answer

Your current while loop has a condition of Thirst.Value >= 0, therefore making it break when the value of Thirst becomes -1 (which it does with its current setup).

A better approach is to create a new loop each time the player respawns. Using the CharacterAdded event, we can accomplish this. As for our while loop, we should set it's condition so that it breaks when the character dies (in other words humanoid.Health > 0).

Now we want one of two things to happen in the while loop. If the thirst value is greater, than zero then we want to decrement it by 1 every second. If the thirst value is anything else (<= 0), then we want the humanoid to take 5 damage every 10 seconds.

Complete Script:

game.Players.PlayerAdded:connect(function(player)
	local Thirst = player.stats.Thirst
	player.CharacterAdded:connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		while humanoid.Health > 0 do
			if Thirst.Value > 0 then
				Thirst.Value = Thirst.Value - 1
				wait(1)
			else
				humanoid:TakeDamage(5)
				wait(10)
			end
		end
	end)
end)

2 comments

0

That still doesn't work :/

0

That is really weird, it works flawlessly when I tested it in studio. Any specifics on what is going on you can give me?

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