Map spawning script not working with FE?

So I’m trying to make the players from each team to spawn at each random part from a spawn group position in each map that spawns. This is the code I have so far in a globalscript in the ServerScriptService:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

local mapd = game.ServerStorage.Maps:FindFirstChild("BasicMap")
local bluespawns = mapd:FindFirstChild("BlueSpawns"):GetChildren()[math.random(1,6)]
local yellowspawns = mapd:FindFirstChild("YellowSpawns"):GetChildren()[math.random(1,6)]

for i, player in ipairs(game.Players:GetChildren()) do
if player.Character and player.Character:FindFirstChild("Torso") then
if player.Team == game:GetService("Teams").Blue then

player.Character:WaitForChild('Torso').CFrame = CFrame.new(bluespawns.Position + 
Vector3.new(0,2.5,0))

elseif player.Team == game:GetService("Teams").Yellow then

player.Character:WaitForChild('Torso').CFrame = CFrame.new(yellowspawns.Position + 
Vector3.new(0,2.5,0))

end
end
end
end)

The script works locally but not with FE/In-game. Should I use RemoteEvents? If so, how do I do that? Any other suggestions or new methods/fixes I could use?

1 Like

Just put the code in a global script in ServerScriptService, that will make it work.

The reason it is not working locally is that you are giving this code to every player when they join, so they try teleporting the players locally, which won’t work at all.

Also I highly suggest you to use the CharacterAdded event and indent the code. It looks like a mess, to be honest.

I forgot to say where the script was. It is already in the ServerScriptService like this:
image
So which function should I start with? CharacterAdded or PlayerAdded?

Try using the CharacterAdded event as I told you, as you not using it is probably what is causing the issue here. In Studio, you get close-to-perfect results so the character is not nil when you play-test and the script runs. However, the server is slower than Studio. Here is the fixed code: (I also cleaned and indented it for you)

local Players = game:GetService("Players")
local mapd = game:GetService("ServerStorage"):WaitForChild("Maps"):WaitForChild("BasicMap")
local bluespawns = mapd:WaitForChild("BlueSpawns"):GetChildren()
local yellowspawns = mapd:WaitForChild("YellowSpawns"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		for i, player in ipairs(game:GetService("Players"):GetPlayers()) do
			local Torso = player.Character:WaitForChild("Torso")
			if player.Team == game:GetService("Teams").Blue then
				local RandomSpawn = bluespawns[math.random(1, #bluespawns)]
				player.Character:WaitForChild('Torso').CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0,2.5,0))
			elseif player.Team == game:GetService("Teams").Yellow then
				local RandomSpawn = yellowspawns[math.random(1, #yellowspawns)]
				player.Character:WaitForChild('Torso').CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0,2.5,0))
			end
		end
	end)
end)

Let me know if the issue still persists.

1 Like

You can also just use Respawn Location property for players or use the team color on the spawn itself.

I believe that the issue more lies in that you should change their position locally. Since the clients ping their location to the server trying to re-position them using a serversided script would be no use as in the next frame the client would ping the old location again. The server does not ping to the local client where his character is.

Which is also why it only works on local tests as the client is the server in that case

Is this the behavior OP wants though? Every time someone resets/dies ALL players on the server get teleported to the spawns.

A word of advice is that you probably want to look for the characters HumanoidRootPart as opposed to just a Torso (As then it will be able to handle R6 and R15 characters compared to just R6).

1 Like

Oh yes, it is because CharacterAdded is fired when any player on the server’s character loads, and the code you wrote just teleports all players, not the player who got their character loaded. This version should work fine:

local Players = game:GetService("Players")
local mapd = game:GetService("ServerStorage"):WaitForChild("Maps"):WaitForChild("BasicMap")
local bluespawns = mapd:WaitForChild("BlueSpawns"):GetChildren()
local yellowspawns = mapd:WaitForChild("YellowSpawns"):GetChildren()

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Torso = player.Character:WaitForChild("Torso")
		if player.Team == game:GetService("Teams").Blue then
			local RandomSpawn = bluespawns[math.random(1, #bluespawns)]
			Character:WaitForChild('Torso').CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0,2.5,0))
		elseif player.Team == game:GetService("Teams").Yellow then
			local RandomSpawn = yellowspawns[math.random(1, #yellowspawns)]
			Character:WaitForChild('Torso').CFrame = CFrame.new(RandomSpawn.Position + Vector3.new(0,2.5,0))
		end
	end)
end)

Let me know if the issue still persists.

1 Like

Yes, thanks this worked!

1 Like