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

Reset GUI function on respawn?

I made a thirst meter that kills you if you don't find water and drink it before it goes to 0, the problem is it keeps killing you after you died once, forcing people to rejoin or leave in confusion, I want it to reset once you die, any tips? This is the script that is supposed to kill you when you run out of thirst.

s = require(workspace.Settings);

screen = script.Parent;
status = screen.Status;
plygui = screen.Parent;
player = plygui.Parent;
thirst = player.Thirst;
dr = player.DehydrationRate;
tt = player.TotalThirst;
thirstGui = status.Thirst;
thirstBG = status.ThirstBG;

while wait(1) do
		if thirst.Value > dr.Value then thirst.Value = thirst.Value - dr.Value; else thirst.Value = 0; end;
		if thirst.Value > tt.Value then thirst.Value = tt.Value; end;
		tprog = thirst.Value / tt.Value;
		thirstGui.Size = UDim2.new(thirstBG.Size.X.Scale, 0, thirstBG.Size.Y.Scale*tprog, 0);
		thirstGui.Position = UDim2.new(thirstBG.Position.X.Scale, 0, thirstBG.Position.Y.Scale+thirstBG.Size.Y.Scale*(1-tprog), 0);
	end;
		if player.Character then
			char = player.Character;
			if player.Character:FindFirstChild("Humanoid") then
				human = char.Humanoid;
				human.Health = human.Health - math.random(s.killRate.low, s.killRate.high);
				human.WalkSpeed = 4 + 12 * human.Health / human.MaxHealth;
				if human.Health < 0 then
					human.Health = 0;
					end
				end;
			end;


4 comments

0

I think you have to add a return after line 27

0

Still nothing, thanks for trying to help.

0

Maybe try and setting the Thirst Value to normal after him being killed?

0

wait() should NOT be a condition.

1 answer

✓ Accepted answer

Your issue is that your code will run infinitely, you never stop killing them even if their health is 0. To fix this, you would need to change your while loop to test if the Character's health is greater than 0. But even then, your code would never run again once the player respawns since a new character model is made. To account for that you would need to put your loop in a .CharacterAdded event.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		-- set all variables/values
		
		local humanoid = character:WaitForChild("Humanoid")
		while humanoid.Health > 0 do
			...
		end
	end)
end)

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