How do I get my functions to run more than once in this loop?

  1. What do you want to achieve? Keep it simple and clear!

I’m making a script that is supposed to teleport 2 players and give them a sword until one player dies 7 times.

  1. What is the issue? Include screenshots / videos if possible!

The functions responsible for respawning the dead player only work once.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried print debugging. I found out that the player variables aren’t losing their values (i believe).

Here is the part of my code that is causing problems:

spawn(function()
	while wait() do
		if player1 ~= nil and player2 ~= nil and player1 ~= player2 and player1ready == true and player2ready == true then
			fighting = true
			local player1deaths = 0
			local player2deaths = 0
			local player1kills = 0
			local player2kills = 0
			player1.Backpack:ClearAllChildren()
			player2.Backpack:ClearAllChildren()
			sword:Clone().Parent = player1.Backpack
			sword:Clone().Parent = player2.Backpack
			player1.Character.Humanoid.Health = 100
			player2.Character.Humanoid.Health = 100
			player1.Character.Torso.CFrame = fightArea.TPLine1.CFrame
			player2.Character.Torso.CFrame = fightArea.TPLine2.CFrame
			player1.Character.Humanoid.Died:Connect(function()
				player1.CharacterAdded:Wait()
				print("1died")
				print(player1)
				wait(1)
				player2kills += 1
				player1deaths += 1
				if player1deaths < 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					sword:Clone().Parent = player1.Backpack
					sword:Clone().Parent = player2.Backpack
					player1.Character.Torso.CFrame = fightArea.TPLine1.CFrame
					player2.Character.Torso.CFrame = fightArea.TPLine2.CFrame
					player1.Character.Humanoid.Health = 100
					player2.Character.Humanoid.Health = 100
					print(100)
				elseif player1deaths == 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					re:FireClient(player1, "battle lost")
					re:FireClient(player2, "battle won")
					player1:LoadCharacter()
					player2:LoadCharacter()
				end
			end)

			player2.Character.Humanoid.Died:Connect(function()
				player2.CharacterAdded:Wait()
				print("2died")
				print(player1)
				wait(1)
				player1kills += 1
				player2deaths += 1
				if player2deaths < 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					player1.Character.Torso.CFrame = fightArea.TPLine1.CFrame
					player2.Character.Torso.CFrame = fightArea.TPLine2.CFrame
					sword:Clone().Parent = player1.Backpack
					sword:Clone().Parent = player2.Backpack
					player1.Character.Humanoid.Health = 100
					player2.Character.Humanoid.Health = 100
				elseif player2deaths == 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					re:FireClient(player1, "battle won")
					re:FireClient(player2, "battle lost")
					player1:LoadCharacter()
					player2:LoadCharacter()
				end
			end)
			repeat wait() until player1deaths == 8 or player2deaths == 8
			print("finishedwaiting")
		end
	end
end)

Can you elaborate on this? I don’t quite understand. Also, please print the player death and kill values for when any player dies so I can see what’s happening.

When I said “losing their values”, I meant that the variables (player1 and player2) aren’t losing the players they reference.

On the other hand, I’ve tried printing the deaths in the function and it turns out that it’s only printed the first time they die. The second time, they simply respawn at a spawn point, no messages are printed, no tools are cloned and the player isn’t teleported.

Is it like they’ve lost the round? Or is it just that single player?

Well, when a player dies they lost the round. And the results are the same for both players after they die twice.

This looks like really choppy code there’s definitely a better way than looping through waits

that being said are you getting any errors? I do notice you use loadcharacters so that could impact the events.

Also why is characteradded under the .died event??? It should be above the function

https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded

player2.CharacterAdded:Connect(function()
player2.Character.Humanoid.Died:Connect(function()
				player2.CharacterAdded:Wait()
				print("2died")
				print(player1)
				wait(1)
				player1kills += 1
				player2deaths += 1
				if player2deaths < 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					player1.Character.Torso.CFrame = fightArea.TPLine1.CFrame
					player2.Character.Torso.CFrame = fightArea.TPLine2.CFrame
					sword:Clone().Parent = player1.Backpack
					sword:Clone().Parent = player2.Backpack
					player1.Character.Humanoid.Health = 100
					player2.Character.Humanoid.Health = 100
				elseif player2deaths == 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					re:FireClient(player1, "battle won")
					re:FireClient(player2, "battle lost")
					player1:LoadCharacter()
					player2:LoadCharacter()
				end
			end)
end)

What would be a better approach to writing this?

Also, I’m not getting any errors.

Finally, good point, I should’ve used something like characterappearance loaded. I wanted the character to be teleported after it was fully loaded in.

local Player1Connection = player1.Humanoid:GetPropertyChangedSignal("Died"):Connect(function()
                player1kills += 1
				player2deaths += 1
				if player2deaths < 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					player1.Character.Torso.CFrame = fightArea.TPLine1.CFrame
					player2.Character.Torso.CFrame = fightArea.TPLine2.CFrame
					sword:Clone().Parent = player1.Backpack
					sword:Clone().Parent = player2.Backpack
					player1.Character.Humanoid.Health = 100
					player2.Character.Humanoid.Health = 100
				elseif player2deaths == 8 then
					player1.Backpack:ClearAllChildren()
					player2.Backpack:ClearAllChildren()
					re:FireClient(player1, "battle won")
					re:FireClient(player2, "battle lost")

                    Player1Connection:Disconnect()

					player1:LoadCharacter()
					player2:LoadCharacter()
				end
end)

I cant verify if this will work but I think it should. Also make an identical version for player 2.

It should also be under characteradded, should also ensure waitforchilds to ensure humanoid has loaded in.

It’s giving me an error message “Unknown global ‘Player1Connection’” when I hover over the disconnect line.

Also, wouldn’t this only run one game? I need it to be able to run forever

i can’t really construct it rn but you should get the jist that you should use onchanged events instead of looping waits

Events | Roblox Creator Documentation.

honestly i dont think it matters whatever works i guess

I found the issue. Basically, everytime the humanoid died, a new character model was loaded in so the player1.Character.Humanoid.Died line kept referring to the old character model. To fix this, I simply changed that part of the line with player1.CharacterAppearanceLoaded.