Why does it give the player 2 swords instead of 1?

Alright, so pretty much the script for some reason gives the player 2 swords. I have no idea whats causing this. Any help is appreciated.

game:GetService("ServerStorage"):FindFirstChild("SwordFightingGamemode").Parent = workspace

local spawnPositions = workspace:FindFirstChild("SwordFightingGamemode").PossibleSpawnPositions
local availableSpawnPoints = spawnPositions:GetChildren()
	
for i,player in pairs(players) do
	if player then
		if player.Character then
			local selectedSpawnPosition = math.random(1,#availableSpawnPoints)

			player.Character:FindFirstChild("HumanoidRootPart").CFrame = availableSpawnPoints[selectedSpawnPosition].CFrame
			table.remove(availableSpawnPoints,selectedSpawnPosition)
			
			local gameTag = Instance.new("BoolValue")
			gameTag.Name = "GameTag"
			gameTag.Parent = player.Character

			script:FindFirstChild("ClassicSword"):Clone().Parent = player.Backpack
			 
			if marketPlaceService:UserOwnsGamePassAsync(player.UserId,214431698) then
				script.Parent.GamepassHandler:FindFirstChild("Boombox"):Clone().Parent = player.Backpack
			end
			
			updatePlayerUiEvent:FireClient(player)
		else
			if not player then
				table.remove(players,i)
			end

			for i,tool in pairs(player.Backpack:GetChildren()) do
				tool:Destroy()
			end
			updatePlayerUiEvent:FireClient(player)
		end
	end
end

Thanks!

1 Like

Can we see more of the code, where players are inserted into the “players” table (aditionally before looping thru the Players just try to print it, so above the for loop print(players)

Another thing “if player then” is completely useless unfortunately since you are already looping thru something, if it didn’t exist the code wouldn’t run so in any case “player” does exist, if you want to remove the player from players table then you’d have to connect it to Players.PlayerRemoving or something else depending on your situation

3 Likes

When does the loop start?
Like does it start when a player joins?

you should also check if the sword exists already before adding it.

if not player.Backpack:FindFirstChild("ClassicSword") then
--Clone the sword
end

*I apologize for my bad english

2 Likes

The problem with removing the “if player then” is that I created the table earlier so its not information thats exactly true (I don’t know how to explain it)
Heres the full script:

local playersService = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local marketPlaceService = game:GetService("MarketplaceService")

local events = replicatedStorage:WaitForChild("Events")
local updatePlayerUiEvent = events:WaitForChild("UpdateUiEvent")

local updateClientIntermissionText = replicatedStorage:WaitForChild("UpdateClientIntermissionText")

local votingMachine = workspace:WaitForChild("GamemodeVoting")
local swordFightingVotor = votingMachine:WaitForChild("SwordFightingVoter")
local arenaVotor = votingMachine:WaitForChild("ArenaVoter")

local function playerAdded(player)
	local vote = Instance.new("StringValue",player)
	vote.Value = "NA"
	vote.Name = "Vote"
	local function characterAdded(character)
		local function characterDied()
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
		end
		character:FindFirstChild("Humanoid").Died:Connect(characterDied)
	end
	player.CharacterAdded:Connect(characterAdded)
end

playersService.PlayerAdded:Connect(playerAdded)

while true do
	for i,song in pairs(workspace.BattleSongs:GetChildren()) do
		song:Stop()
	end
	workspace:FindFirstChild("intermissionsong"):Play()
	
	if playersService.NumPlayers <= 1 then
		updateClientIntermissionText.Value = "Could not start game: 2 or more players needed."
		repeat task.wait(1) until playersService.NumPlayers >= 2
	end
	
	local function updateVotesDisplay()
		votingMachine.SwordFightingPart.SurfaceGui.Votes.Text = "Votes: "..swordFightingVotor.Votes.Value
		votingMachine.ArenaPart.SurfaceGui.Votes.Text = "Votes: "..arenaVotor.Votes.Value
		print("Votes: "..arenaVotor.Votes.Value)
	end

	for i = 40,1,-1 do
		for i,votingPad in pairs(votingMachine:GetChildren()) do
			if votingPad:FindFirstChild("Votes") then
				votingPad.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") then
						local character = hit.Parent
						local player = playersService:GetPlayerFromCharacter(character)

						if player.Vote.Value ~= votingPad then
							votingPad.Votes.Value = votingPad.Votes.Value + 1
							if player.Vote.Value ~= "NA" then
								local padToSubtract = votingMachine:FindFirstChild(player.Vote.Value)
								padToSubtract.Votes.Value = padToSubtract.Votes.Value - 1
							end
							player.Vote.Value = votingPad.Name
							updateVotesDisplay()
						end
					end
				end)
			end
		end

		updateClientIntermissionText.Value = "Intermission: "..i.." seconds left"
		task.wait(1)
	end
	
	local function calculateWinner()
		local gamemodeVotes = {["Sword Fighting"] = swordFightingVotor.Votes.Value,["Arena"] = arenaVotor.Votes.Value}
		
		local highest = 0
		local gamemode = nil
		
		for i,v in pairs(gamemodeVotes) do
			if v >= highest then
				highest = v
				gamemode = i
			end
		end
		
		return gamemode
	end
	
	local winner = calculateWinner()
	
	if winner == nil then
		winner = "Arena"
	end
	
	local votedGamemode = winner

	workspace:FindFirstChild("intermissionsong"):Stop()
	updateClientIntermissionText.Value = "Starting game: "..votedGamemode
	wait(5)
	
	workspace.BattleSongs:GetChildren()[math.random(1,#workspace.BattleSongs:GetChildren())]:Play()

	local players = {}

	for i,player in pairs(playersService:GetPlayers()) do
		if player then
			table.insert(players,player)
			for i,tool in pairs(player.Backpack:GetChildren()) do
				tool:Destroy()
			end
		end
	end
	
	if votedGamemode == "Arena" then
		game:GetService("ServerStorage"):FindFirstChild("ArenaGamemode").Parent = workspace
		
		wait(9.9)
		
		local spawnPositions = workspace:FindFirstChild("ArenaGamemode").PossibleSpawnPositions
		local availableSpawnPoints = spawnPositions:GetChildren()
		for i,player in pairs(players) do
			if player then
				if player.Character then
					print("ok")
					
					local selectedSpawnPosition = math.random(1,#availableSpawnPoints)

					player.Character:FindFirstChild("HumanoidRootPart").CFrame = availableSpawnPoints[selectedSpawnPosition].CFrame
					table.remove(availableSpawnPoints,selectedSpawnPosition)
					
					local gameTag = Instance.new("BoolValue")
					gameTag.Name = "GameTag"
					gameTag.Parent = player.Character
					
					for i,tool in pairs(script:GetChildren()) do
						tool:Clone().Parent = player.Backpack
					end
					
					if marketPlaceService:UserOwnsGamePassAsync(player.UserId,214431698) then
						script.Parent.GamepassHandler:FindFirstChild("Boombox"):Clone().Parent = player.Backpack
					end
					
					updatePlayerUiEvent:FireClient(player)
				else
		--			if not player then
			--			table.remove(players,i)
			--		end
					
					for i,tool in pairs(player.Backpack:GetChildren()) do
						tool:Destroy()
					end
					updatePlayerUiEvent:FireClient(player)
				end
			end
		end
		
		for i = 180,1,-1 do
			for index2,player in pairs(players) do
				if player then
					character = player.Character
					if character then
						if not character:FindFirstChild("GameTag") then
							table.remove(players,index2)
						end
					end
				else
					table.remove(players,index2)
				end
			end
			updateClientIntermissionText.Value = "Game: "..i.." seconds left"
			
			if #players == 1 then
				updateClientIntermissionText.Value = "Winner: "..players[1].Name
				players[1]:FindFirstChild("leaderstats").Wins.Value = players[1]:FindFirstChild("leaderstats").Wins.Value + 1
				wait(5)
				break
			end
			
			wait(1)
		end
		if #players >= 2 then
			updateClientIntermissionText.Value = "Winner: absolutly nobody"
			wait(5)
		end
		for i,player in pairs(playersService:GetPlayers()) do
			character = player.Character
			if character then
				if character:FindFirstChild("GameTag") then
					character.GameTag:Destroy()
				end
			end
			player.Character.Humanoid.Health = 0
		end
		
		workspace:FindFirstChild("ArenaGamemode").Parent = game:GetService("ServerStorage")
	elseif votedGamemode == "Sword Fighting" then
		game:GetService("ServerStorage"):FindFirstChild("SwordFightingGamemode").Parent = workspace

		local spawnPositions = workspace:FindFirstChild("SwordFightingGamemode").PossibleSpawnPositions
		local availableSpawnPoints = spawnPositions:GetChildren()
		
		for i,player in pairs(players) do
			if player then
				if player.Character then
					local selectedSpawnPosition = math.random(1,#availableSpawnPoints)

					player.Character:FindFirstChild("HumanoidRootPart").CFrame = availableSpawnPoints[selectedSpawnPosition].CFrame
					table.remove(availableSpawnPoints,selectedSpawnPosition)
					
					local gameTag = Instance.new("BoolValue")
					gameTag.Name = "GameTag"
					gameTag.Parent = player.Character

					script:FindFirstChild("ClassicSword"):Clone().Parent = player.Backpack
					 
					if marketPlaceService:UserOwnsGamePassAsync(player.UserId,214431698) then
						script.Parent.GamepassHandler:FindFirstChild("Boombox"):Clone().Parent = player.Backpack
					end
					
					updatePlayerUiEvent:FireClient(player)
				else
					if not player then
						table.remove(players,i)
					end

					for i,tool in pairs(player.Backpack:GetChildren()) do
						tool:Destroy()
					end
					updatePlayerUiEvent:FireClient(player)
				end
			end
		end
		
		for i = 60,1,-1 do
			for index2,player in pairs(players) do
				if player then
					character = player.Character
					if character then
						if not character:FindFirstChild("GameTag") then
							table.remove(players,index2)
						end
					end
				else
					table.remove(players,index2)
				end
			end
			updateClientIntermissionText.Value = "Game: "..i.." seconds left"

			if #players == 1 then
				updateClientIntermissionText.Value = "Winner: "..players[1].Name
				players[1]:FindFirstChild("leaderstats").Wins.Value = players[1]:FindFirstChild("leaderstats").Wins.Value + 1
				wait(5)
				break
			end

			wait(1)
		end
		if #players >= 2 then
			updateClientIntermissionText.Value = "Winner: absolutly nobody"
			wait(5)
		end
		for i,player in pairs(playersService:GetPlayers()) do
			character = player.Character
			if character then
				if character:FindFirstChild("GameTag") then
					character.GameTag:Destroy()
				end
			end
			player.Character.Humanoid.Health = 0
		end
		
		workspace:FindFirstChild("SwordFightingGamemode").Parent = game:GetService("ServerStorage")
	end
end
1 Like

Try using print statements when the swords parent is changed to the backpack, it will help you narrow it down. Alternatively if nothing works you can add the player in a debounce table for 2 seconds after it clones the first time

1 Like