Respawns several times... Need help!

You can write your topic however you want, but you need to answer these questions:
Hey everyone! Hope you are having a good day :slight_smile:!

I have an issue with this script:
This game has ghosts and humans

Whenever a human dies they turn into a ghost but whenever the character respawns it respawns like 5-7 times… I just want it to spawn once but I can’t seem to figure out a way to do it :confused:

function module.StartRound(length, chosenMap) 

	local contestants = {}
	local ghosts = {}

	local debounce = false
	local debounce2 = false

--Script is not working because of timer with actions

	game.Players.CharacterAutoLoads = false
	game.Players.RespawnTime = 5
		for i = length,0,-1 do -- I think the problem is here

			status.Value = toMS(i)
			wait(1)	
		
		
		
		for i, player in pairs(game.Players:GetPlayers()) do
			if player:FindFirstChild("Contestant") then
				table.insert(contestants, player)
				local humanoid = player.Character.Humanoid
				humanoid.Died:Connect(function()
					player:FindFirstChild("Contestant"):Destroy()
					local Tag = Instance.new("StringValue")
					Tag.Name = "Ghost"
					Tag.Parent = player
					table.insert(ghosts, player)


					local Teams = game:GetService("Teams")

					local ghostteam = Teams.Ghosts
					local humanteam = Teams.Humans

					player:LoadCharacter()

					player.Team = ghostteam

					local newghost = game.ServerStorage.GameGhost:Clone()

					newghost.Name = player.Name

					player.Character = newghost

					newghost.Parent = workspace

					local mapSpawns = game.Workspace.MapHolder.Map.Spawns.Ghosts:GetChildren()
					local rand = Random.new()


					player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1, #mapSpawns)].CFrame + Vector3.new(0,2,0) 


					player.PlayerGui.GhostGUI.Enabled = true

					if player.PlayerGui.GhostGUI.Enabled == true then
						game.ServerStorage.GameGhost.LocalScript.Disabled = false
					end
				end)

			elseif player:FindFirstChild("Ghost") then
				local humanoid = player.Character.Humanoid
				humanoid.Died:Connect(function()
					local respawntime = 2
					wait(respawntime)


					player:LoadCharacter()

					local newghost = game.ServerStorage.GameGhost:Clone()

					newghost.Name = player.Name

					player.Character = newghost

					newghost.Parent = workspace

					local mapSpawns = game.Workspace.MapHolder.Map.Spawns.Ghosts:GetChildren()
					local rand = Random.new()


					player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1, #mapSpawns)].CFrame + Vector3.new(0,2,0) 


					player.PlayerGui.GhostGUI.Enabled = true

					if player.PlayerGui.GhostGUI.Enabled == true then
						game.ServerStorage.GameGhost.LocalScript.Disabled = false
					end
				end)
			end

end


		end
		
		
		end

I see the issue - you also saw it too!

The issue is you have a nested for loop - specifically the for i, player in ... line - you need to put that for loop for that outside the for loop which it is located in - specifically: for i = length, 0, -1 do.

Also for readability I’d suggest formatting your code a little better - but besides that, looks okay!

1 Like

Hey! Thanks for the feedback and sorry bout the mess haha, sadly the script is still having the same problem even with your solution :confused:

1 Like

Hey there! Try changing the -1 to 1 and then let me know what happens.

A for loop is a counting loop that goes from a starting number to an ending number. You assign a variable to be the placeholder for the current number you are on. These loops work kind of like while loops, but with a little less code, and a lot more control. A for loop needs three arguments:

  • Start: the number you want to start counting from.

  • End: number you are trying to get to.

  • Step: the interval to count by.

–Credit to CodePrime8 for this for loop explanation.

The reason I am saying change the -1 to 1 is because:

--Look at this for loop.
for i = 1, 10 , 1
print(x)
end)

This will print out numbers counting from 1 to 10 and stopping. Now look at the next for loop:

--Look at this for loop.
for i = 1, 10 , -1
print(x)
end)

This will print out absolutely nothing, which is why I believe your -1 is the issue.

1 Like

Hey there,
The reason this is occuring is due to where you have placed your end blocks. This is causing this to run for as long as the length variable, which is 5 (i think).
I’d suggest doing this:

function module.StartRound(length, chosenMap) 

	local contestants = {}
	local ghosts = {}

	local debounce = false
	local debounce2 = false

--Script is not working because of timer with actions

	game.Players.CharacterAutoLoads = false
	game.Players.RespawnTime = 5
		for i = length,0,-1 do -- I think the problem is here

			status.Value = toMS(i)
			wait(1)	
		end
		
		
		
		for i, player in pairs(game.Players:GetPlayers()) do
			if player:FindFirstChild("Contestant") then
				table.insert(contestants, player)
				local humanoid = player.Character.Humanoid
				humanoid.Died:Connect(function()
					player:FindFirstChild("Contestant"):Destroy()
					local Tag = Instance.new("StringValue")
					Tag.Name = "Ghost"
					Tag.Parent = player
					table.insert(ghosts, player)


					local Teams = game:GetService("Teams")

					local ghostteam = Teams.Ghosts
					local humanteam = Teams.Humans

					player:LoadCharacter()

					player.Team = ghostteam

					local newghost = game.ServerStorage.GameGhost:Clone()

					newghost.Name = player.Name

					player.Character = newghost

					newghost.Parent = workspace

					local mapSpawns = game.Workspace.MapHolder.Map.Spawns.Ghosts:GetChildren()
					local rand = Random.new()


					player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1, #mapSpawns)].CFrame + Vector3.new(0,2,0) 


					player.PlayerGui.GhostGUI.Enabled = true

					if player.PlayerGui.GhostGUI.Enabled == true then
						game.ServerStorage.GameGhost.LocalScript.Disabled = false
					end
				end)

			elseif player:FindFirstChild("Ghost") then
				local humanoid = player.Character.Humanoid
				humanoid.Died:Connect(function()
					local respawntime = 2
					wait(respawntime)


					player:LoadCharacter()

					local newghost = game.ServerStorage.GameGhost:Clone()

					newghost.Name = player.Name

					player.Character = newghost

					newghost.Parent = workspace

					local mapSpawns = game.Workspace.MapHolder.Map.Spawns.Ghosts:GetChildren()
					local rand = Random.new()


					player.Character.HumanoidRootPart.CFrame = mapSpawns[rand:NextInteger(1, #mapSpawns)].CFrame + Vector3.new(0,2,0) 


					player.PlayerGui.GhostGUI.Enabled = true

					if player.PlayerGui.GhostGUI.Enabled == true then
						game.ServerStorage.GameGhost.LocalScript.Disabled = false
					end
				end)
			end

end


		
		
		end

This way, your end is in the correct placement and the errors should be gone.

He’s counting from a larger number to zero - the iterator is perfectly valid, not an issue.

He says even with my suggestion (which is a part of the cause) that it is still happening.

Another thing to check for @alex0393044 is to check where else you call your StartRound method - check to make sure it is only called when you need it, and try adding a print to the beginning of the function to check how many times it is being called, as well as in the loop where you respawn the characters as an extra debugging step.