Only 1 player is getting teleported to a map

Continuing the discussion from 1 player is being teleported only:

I am trying to make a functioning game, but when it teleports the players, only 1 player is teleported. I have tried putting in a wait before the game starts, but that does not work either. Any help would be appreciated.

Script

– Last updated: 2/21/22

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local mapsFolder = ServerStorage:WaitForChild("Maps")
local statusTag = ReplicatedStorage:WaitForChild("Status")

-- Constants
local GAME_LENGTH = 120
local REWARD = 1
local REWARD_2 = 50
local PLAYERS_REQUIRED = 2
local INTERMISSION_TIME = 15

-- Functions
local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			if CollectionService:HasTag(player, "Alive") then
				CollectionService:RemoveTag(player, "Alive")
			end
		end)
	end
	if player.Character then
		onCharacterAdded(player.Character)
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

-- Main loop
while true do
	-- Waiting for players
	statusTag.Value = "Waiting for enough players"
	repeat
		task.wait(1)
	until
	#Players:GetPlayers() >= PLAYERS_REQUIRED
	
	wait(3)
	-- Intermission
	for i = INTERMISSION_TIME, 0, -1 do
		statusTag.Value = "Intermission ["..i.."]"
		task.wait(1)
	end

	-- Choose a map
	local allMaps = mapsFolder:GetChildren()
	local map = allMaps[math.random(1,#allMaps)]:Clone()
	statusTag.Value = map.Name .. " was chosen"
	map.Parent = workspace
	task.wait(4)
	
	-- Teleport players
	local activePlayers = Players:GetPlayers()
	local spawns = map:FindFirstChild("SpawnPoints")
	if not spawns then
		warn("You do not have 'SpawnPoints', fix pl0x")
	end
	spawns = spawns:GetChildren()
	
	for _, player in pairs(activePlayers) do
		local name = player.Name
		local body = workspace:FindFirstChild(name)
		local hrp = body:WaitForChild("HumanoidRootPart")
		
		if hrp then
			local chosenSpawn = spawns[math.random(#spawns)]
			hrp.CFrame = chosenSpawn.CFrame
			table.remove(spawns, table.find(spawns, chosenSpawn))

			-- Give them a sword
			local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
			
			if equipped ~= "" then
				local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
				weapon.Parent = player.Backpack
			else
				local sword = ServerStorage.ClassicSword:Clone()
				sword.Parent = player.Backpack
			end
			
			

			CollectionService:AddTag(player, "Alive")
		end
	end

	statusTag.Value = "Get ready to play!"
	task.wait(2)
	for i = GAME_LENGTH, 0, -1 do
		if #CollectionService:GetTagged("Alive") == 1 then
			-- only one player is alive
			local winner = CollectionService:GetTagged("Alive")[1]
			statusTag.Value = "The winner is "..winner.Name
			winner.leaderstats.Wins.Value += REWARD
			winner.leaderstats.Coins.Value += REWARD_2
			break
		elseif #CollectionService:GetTagged("Alive") == 0 then
			-- nobody is alive
			statusTag.Value = "Nobody won!"
			break
		elseif i == 0 then
			-- timer ran out
			statusTag.Value = "Times up!"
			break
		end
		task.wait(1)
		statusTag.Value = "There are " .. i .. " seconds remaining, and " .. #CollectionService:GetTagged("Alive") .. " players left"
	end

	print("END OF GAME")

	-- Clean up
	for _, player in pairs(activePlayers) do
		if CollectionService:HasTag(player, "Alive") then
			CollectionService:RemoveTag(player, "Alive")
		end
		
		for _, tool in pairs(player.Backpack:GetChildren()) do
			if tool:FindFirstChild("Price") then
				tool:Destroy()
			end
		end
		
		for _, tool in pairs(player.Character:GetChildren()) do
			if tool:FindFirstChild("Price") then
				tool:Destroy()
			end
		end
		player:LoadCharacter()
	end

	map:Destroy()
	statusTag.Value = "Game over"
	task.wait(2)
end
1 Like

Try Players:GetChildren() instead of Players:GetPlayers().

That does not work either, also I have a video for my problem. Click here to see the video.

Try using ipairs in the for loop for teleporting the players instead.

No, that doesn’t work when I test it.

Could u perhaps have any dummies in the workspace with your name and no hrp?

So I should insert 2 dummies in with the same name?

No im asking if that might be the case it might confuse the players character with the dummy with the same name and yield on the waitforchild()
You could try using player.Character instead of searching for the body through workspace with the name

Oh, okay. No, I don’t have anything in there other than one with no HRP just named " ".

local hrp = body:FindFirstChild("HumanoidRootPart")

FindFirstChildchecks if a child exists, not WaitForChild, it will just wait until a humanoid root part exists.

Also add an else to if hrp then to see if it could be an issue with the character.

	for _, player in pairs(activePlayers) do
		local Character = player.Character
        if not Character then
           continue
        end
		local chosenSpawn = spawns[math.random(1, #spawns)]
		Character:MoveTo(chosenSpawn.CFrame)
		table.remove(spawns, table.find(spawns, chosenSpawn))
		-- Give them a sword
		local equipped = game.ServerStorage.PlayerData[player.Name].Equipped

		if equipped ~= "" then
			local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
			weapon.Parent = player.Backpack
		else
			local sword = ServerStorage.ClassicSword:Clone()
			sword.Parent = player.Backpack
		end
			
		CollectionService:AddTag(player, "Alive")
	end

Now nobody gets teleported in the game when I start it.

Are there any errors? If there aren’t, use debug methods (prints) to find where the script is breaking.

have you made sure there’s enough spawn points for everyone?

Yes, I have made sure there are enough spawn points.

So does putting it in a pcallwork?

You should try doing local Character = player.Character or player.CharacterAdded:Wait() from the script sam posted to just debug if its a char loading problem

one question what does your output say?

pcall will make it so the loop will continue even if it breaks. This would be useless for my solution because it apparently doesn’t work once, but might work for your original code. This will be a bad fix though because if something is erroring, it’ll mean the script isn’t working in the way you want it to. What’s in your output for my solution? What is PlayerData in ServerStorage anyways?

game.ServerStorage.PlayerData[player.Name].Equipped

I tested your script and my guess is that this is the problem

76. -- Give them a sword
77. local equipped = game.ServerStorage.PlayerData[player.Name].Equipped
78
79. if equipped ~= "" then
80. 	local weapon = game.ServerStorage.Items[equipped.Value]:Clone()
81. 	weapon.Parent = player.Backpack
82. else
83. 	local sword = ServerStorage.ClassicSword:Clone()
84. 	sword.Parent = player.Backpack
85. end

I think on line 79 you should change equipped ~= "" to equipped.Value ~= ""

2 Likes