How to accurately check if a player died in a repeat loop?

Hey!
The title says it all, I desperately need help with this.

Any tutorials and/or code would be much appreciated!

Thank you!

2 Likes

I wouldn’t use a repeat loop, but incase you must here is how you can go about it:

repeat
wait()
until game.Players.LocalPlayer.Character.Humanoid.Health <= 0

You really should use .Died event.

Player.Character.Humanoid.Died:Connect(function()
 -- To Do
end)
1 Like

This is why I need a repeat loop:

repeat
	wait(1)

	s.Value = "Keymaster, complete tasks to win the game!"
	T.Value = Ti.Value

	Ti.Value -= 1
	RemainingTime = Ti.Value

	local KEY = game.Players:FindFirstChild(KeymasterPlr.Name)
	local HUNT = game.Players:FindFirstChild(HunterPlr.Name)

	if not KEY then
		Ti.Value = 0
		Winner = "Player Left"
		KMAlive = false
		EndGame = true
	end
	
	if KEY then
		workspace[KeymasterPlr.Name].Humanoid.Died:Connect(function()
			print("Keymaster died")
			Ti.Value = 0
			ForceStop = true
			EndGame = true
			Winner = "Hunter Win"
		end)
	end

	if HUNT == nil then
		Ti.Value = 0
		Winner = "Player Left"
		HAlive = false
		EndGame = true
	end

until Ti.Value <= 0 or EndGame == true or ForceStop == true or script.EndGame.Value == true

(This is the code that im having issues with.)
it sometimes works, but sometimes breaks.

1 Like

Ok, if that’s the case it’s just a simple conditional like so:

Player.Character.Humanoid.Health <= 0 then
-- TODO
end

I’m just concerned, what happens if the player isn’t in game?
EDIT: Won’t it show an error and not work?

1 Like
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid =  Character.Humanoid

repeat
wait()
until Humanoid.Died

Not sure if this will work since I wrote it from the top of my head. Let me know if it does.

1 Like

Good question! Probably should’ve been something I mentioned, I would modify the if statement like so:

if Player and Player.Character.Humanoid.Health <= 0 then
-- TODO This will first check if the player is not nil, if it isn't it will go and begin checking the humanoid's health.
elseif not Player then
-- TODO No player let's end the game here too.
end

EDIT: Theoretically, your code has already covered this issue with this snippet:

	if not KEY then
		Ti.Value = 0
		Winner = "Player Left"
		KMAlive = false
		EndGame = true
	end
1 Like

Ill try both of these out, thanks guys!

1 Like

Ok, so it didn’t work. which is odd. ill keep experimenting with it, if you have any other ideas feel free to let me know : D

1 Like

It should work, the reason why .Died event doesn’t work in your original code is quite dangerous.
Every second you’ll be creating a new connection .Died. Say the humanoid dies after 500 seconds, that block of code will theoretically execute 500 times. Furthermore, since you’re calling it every time you don’t give a chance for the humanoid to die afterwards.

repeat
	wait(1)

	s.Value = "Keymaster, complete tasks to win the game!"
	T.Value = Ti.Value

	Ti.Value -= 1
	RemainingTime = Ti.Value

	local KEY = game.Players:FindFirstChild(KeymasterPlr.Name)
	local HUNT = game.Players:FindFirstChild(HunterPlr.Name)

	if not KEY then
		Ti.Value = 0
		Winner = "Player Left"
		KMAlive = false
		EndGame = true
	end
	
	if KEY then
               if KEY.Character then
                     if KEY.Character:FindFirstChild.Humanoid.Health <= 0 then
                        print("Keymaster died")
			Ti.Value = 0
			ForceStop = true
			EndGame = true
			Winner = "Hunter Win"
                     end
               end
	end

	if HUNT == nil then
		Ti.Value = 0
		Winner = "Player Left"
		HAlive = false
		EndGame = true
	end

until Ti.Value <= 0 or EndGame == true or ForceStop == true or script.EndGame.Value == true
2 Likes

Thank you! it works well now! I’ll let you know if I experience any more issues!

1 Like