While loop issue

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
1 Like

You may use wait(3) before the kick function

2 Likes

I’ve tested this and it doesn’t work.
It doesn’t run the kick function at all and the player dies before they get kicked

1 Like

What exactly do you want to achieve with it, like if they die, they get kicked?

2 Likes

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

1 Like

Maybe try this?

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)
2 Likes

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 
1 Like

Same problem as last time, runs the kick too early.
This repeat loop also nearly crashes roblox studio for some reason

1 Like

Have you tried mine? It should work, I think

2 Likes

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

1 Like

Huh, by the Roblox System the Player is getting healed, or do you mean, if the Player get revived, he still gets kicked?

1 Like
script.Parent.Humanoid.Health = 100
script.Parent.Humanoid.WalkSpeed = 16
script.Parent.Humanoid.JumpPower = 52
loadanim:Stop()

This line of code runs afterwards and that’s what is healing the player
I disabled Roblox’s heal

So whats wrong then, I mean the Player is getting kicked if they are down for three Seconds. So what do you mean by heal then?

So you are running that section of code after the player gets kicked?

You need to show us the code that kicks the player as well as the the healing section of code so we can tell what isn’t working properly.


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)

This is the full code

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.

1 Like
  1. Please show how you activate the loop, you might just be activating it multiple times.
  2. Please show the count ValueObject.
  3. 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
2 Likes

Thank you so much for this help, works perfectly!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.