For a strange reason, this while loop is not adding the count value correctly and it keeps kicking the player out early.
Also, I’ve added print statements to debug after the if statement and for a strange reason they don’t even run yet it kicks the player.
I’m so confused and I’ve tried moving the task.wait() around and switching the count value from a variable to an int value.
It’s meant to wait three seconds before kicking the player out but it doesn’t do that.
while true do
task.wait(0.1)
count.Value +=1
script.Parent.Humanoid.Health = 10
if count.Value == 30 then
game.Players:GetPlayerFromCharacter(script.Parent):Kick("You have died.")
break
end
if dying.Value == false then
break
end
end
No, at ten health I want it so they have three seconds before they get kicked.
It’s meant to be thirty seconds but I’ve set it to three for testing purposes.
It’s only at ten health before they get kicked to allow for a revive mechanic which is why the while loop checks if dying = false
script.Parent:FindFirstChild("Humanoid").HealthChanged:Connect(function(Heart)
if Heart == 10 then
if dying.Value == true then
game.Players:GetPlayerFromCharacter(script.Parent):Kick("You have died.")
else
end
end
end)
Have you tried using Repeat Until loop instead of while? Maybe check the count.Value as the condition.
repeat
task.wait(0.1)
count.Value +=1
script.Parent.Humanoid.Health = 10
if count.Value == 30 then
game.Players:GetPlayerFromCharacter(script.Parent):Kick("You have died.")
break
end
if dying.Value == false then
break
end
until count.Value>30
It works and kicks the player on time but heals the player before it kicks them?
I’ll try fix that though and see what’s going on
Thank you for your help, I’ll mark it as a solution once I fix that issue
local dying = Instance.new("BoolValue")
dying.Name = script.Parent.Name .. " DYING"
dying.Parent = game.ServerStorage
script.Parent.Humanoid.MaxHealth = 110
script.Parent.Humanoid.Health = 110
local count = script.Value
local animid = "rbxassetid://13372717807"
script.Parent.Humanoid.Changed:Connect(function()
if script.Parent.Humanoid.Health <= 10 then
dying.Value = true
local animation = Instance.new("Animation")
animation.AnimationId = animid
local loadanim = script.Parent.Humanoid.Animator:LoadAnimation(animation)
loadanim:Play()
script.Parent.Humanoid.WalkSpeed = 0
script.Parent.Humanoid.JumpPower = 0
script.Parent:FindFirstChild("Humanoid").HealthChanged:Connect(function(Heart)
if Heart == 10 then
dying.Value = true
loadanim:Play()
script.Parent.Humanoid.WalkSpeed = 0
script.Parent.Humanoid.JumpPower = 0
while true do
task.wait(1)
script.Parent.Humanoid.Health = 10
count.Value +=1
if count.Value == 30 then
break
end
if dying.Value == false then
break
end
end
end
end)
if count.Value == 30 then
game.Players:GetPlayerFromCharacter(script.Parent):Kick("You have died.")
end
script.Parent.Humanoid.Health = 100
script.Parent.Humanoid.WalkSpeed = 16
script.Parent.Humanoid.JumpPower = 52
loadanim:Stop()
end
end)
Before each of your if try putting print statements for the item that’s being checked so you can see what the numbers are.
For example if you have a line like if Heart == 10 then it could possibly be due to floating point error in calculations that Heart may equal 9.999999999, which would fail that if check.
For example putting print(Heart) if Heart == 10 then
would help you troubleshoot any issue when you do it before all your if statements.
Please show how you activate the loop, you might just be activating it multiple times.
Please show the count ValueObject.
Also honestly ignore the top few points and just do the following:
Also note, I suggest to rather use RunService, because it counts the actual change in time. task.wait() and wait() always wait longer than you tell them to, either from a few miliseconds to a while depending on the lag.
for i=1,30 do
task.wait(0.1)
if dying.Value == false then
break
end
if i == 30 then
game.Players:GetPlayerFromCharacter(script.Parent):Kick("You have died.")
break
end
end