Breaking a loop when the player dies

  1. What do you want to achieve? I want to break a loop once the player dies

  2. What is the issue? Using Humanoid.Died hasn’t worked

  3. What solutions have you tried so far? I have looked at a few posts but nothing has helped

I’m making a game similar to blade ball and I have come across this issue. I am making a loop for the ball to follow the player but I want to stop it whenever the player dies so I tried to use the break word on a Humanoid.Died function.

game.Players.PlayerAdded:Connect(function(Player)
	
	local Ball = workspace.Ball
	
	for _,v in pairs(workspace:GetChildren()) do
		if v:IsA("Part") and v.Name == "PlayerDetecters" then
			v:Destroy()
		end
	end
	
	local Part = Instance.new("Part")
	Part.Name = "PlayerDetecters"
	Part.Parent = workspace
	Part.Anchored = false
	Part.CanCollide = false
	Part.Size = Vector3.new(999999999999,999999999999,999999999999)
	Part.CFrame = Ball.CFrame
	
	local con
	
	con = Part.Touched:Connect(function(hit)
		local enemy = hit.Parent:FindFirstChild("Humanoid")
		if not hit.Parent:FindFirstChild("Humanoid") then return end
		if enemy then
			con:Disconnect()
			print(hit.Parent)
			
			local Client = Player
			local object = game.Workspace.Ball
			local speed = 80
			
			while true do
				
				local playerPos = Client.Character.HumanoidRootPart.Position
				local objectPos = object.Position
				local direction = (playerPos - objectPos).Unit
				local distance = (playerPos - objectPos).Magnitude

				if distance > 1 then 
					object.CFrame = object.CFrame + direction * speed * math.min(distance, 1/60)
				end
				
				
				
				enemy.Died:Connect(function()
					break
				end)
				
				wait()
				
			end
			
		end
	end)
 end)

Apparently loops can only be stopped inside the loop but how would I do it if I want to stop it once the player dies?

Edit: Sorry for my bad english, it is not my first language

2 Likes

While loops are conditional loops. This means that they run on the condition of something. I rearranged your loop, so instead of repeating your code infinitely (with the condition that true is equal to true) I opted for a ‘health check’:

while enemy.Health > 0 do -- we're checking if the health of the Humanoid is greater than 0, which means they're alive
	local playerPos = Client.Character.HumanoidRootPart.Position
	local objectPos = object.Position
	local direction = (playerPos - objectPos).Unit
	local distance = (playerPos - objectPos).Magnitude

	if distance > 1 then 
		object.CFrame = object.CFrame + direction * speed * math.min(distance, 1/60)
	end

	task.wait()
end

If for whatever reason this doesn’t work. You can try this:

while true do
	local playerPos = Client.Character.HumanoidRootPart.Position
	local objectPos = object.Position
	local direction = (playerPos - objectPos).Unit
	local distance = (playerPos - objectPos).Magnitude

	if distance > 1 then 
		object.CFrame = object.CFrame + direction * speed * math.min(distance, 1/60)
	end
	
	if (enemy.Health <= 0) then
		break
	end

	task.wait()
end
1 Like

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