--Pointing out bad practices is appreciated!
while task.wait(1) and TimeToCount > 0 do
TimeToCount -= 1
CountdownSound:Play()
countdown.Text = TimeToCount .." seconds"
if characater:FindFirstChildOfClass("Humanoid").Health == 0 then
print("a")
break
end
end
It’s just a countdown that runs for 10 seconds then returns the player to spawn through a RemoteEvent, activating a Script that changes the character’s CFrame to the spawn’s.
Now, if a player is killed mid-countdown, the loop will continue running. I want to avoid that, so I added this if staement:
if characater:FindFirstChildOfClass("Humanoid").Health == 0 then
print("a")
break
end
For some reason, even when Humanoid.Health is 100, the if checks true, “a” prints and the loop breaks, when it should only happen if Humanoid.Health == 0 instead.
I tried doing Humanoid.Died:Connect(function()) but the break won’t work since it has to be directly inside a loop.
I want:
To know why this happens
A solution that breaks the loop when the player is killed mid countdown.
You could add a flag variable, initially set to false, which is set to true when humanoid.died fires. Then, inside your loop, you just break when said variable is true.
well what i would do is to add the player health conditional in the while line, like this:
local Humanoid = characater:FindFirstChildOfClass("Humanoid")
while task.wait(1) and TimeToCount > 0 and Humanoid.Health ~= 0 do
TimeToCount -= 1
CountdownSound:Play()
countdown.Text = TimeToCount .." seconds"
end
end
so after iterating again, the health will be checked, and if its dead, the loop wont continue,
if the player health already changed while doing an interation, i would do what @tacovaIk said.
first: Use FindFirstChild Humanoid, it’s always named like that
second: You can loop your sound every 1 second, there is property for that soo no need to play it every repeat
third: to fix your issue i would advice to create loop end condition
local condition = false -- our condition
character.Humanoid.Died:Once(function() -- i suppose that if you break the loop, you need it only once a game, soo i added Once instead of connect
condition = true -- breaks the loop
end)
repeat
TimeToCount -= 1
countdown.Text = TimeToCount.." seconds"
task.wait(1)
until condition or TimeToCount <= 0 -- i used repeat because it looks better in this scenario
This could be your problem then: the character variable is defined to the the player’s first character. So for example, if the player dies, then respawns, the character variable won’t be updated to the respawned character. The first character died, so the function would return true despite the new character model being alive.
You can use a function like this to check if the player is alive or not:
local function isPlayerAlive(player)
local character = player.Character
if not character then
return false -- The player has no character model
end
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
return false -- The player's character has no humanoid
end
if humanoid.Health <= 0 then
return false -- The humanoid has 0 health
end
return true -- Otherwise the character is alive
end
The function above uses the player’s current character model, instead of the stored value for their first character model. You could use that function in place of characater:FindFirstChildOfClass("Humanoid").Health == 0.
Whether or not this is the code’s main issue, this is an important case to handle.