Players aren't getting teleported?

I have this issue in my game where sometimes players don’t get teleported to the map
the player:GetAttribute(“Trading”) just detects the player is fully loaded

The error usually happens when the player lags but I’m not sure.
Any help is appreciated.

	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Character and player:GetAttribute("Trading") ~= nil then
			local randomPart = spawns:GetChildren()[math.random(1, #spawns:GetChildren())]
			player:SetAttribute("Alive",true)
			player:SetAttribute("Moneys",0)
			player.Character.HumanoidRootPart.CFrame = (randomPart.CFrame)
			player:SetAttribute("Role","Innocent")
		end
	end

I use a different way for moving players which I find cleaner, try using:

	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Character and player:GetAttribute("Trading") ~= nil then
			local randomPart = spawns:GetChildren()[math.random(1, #spawns:GetChildren())]
			player:SetAttribute("Alive",true)
			player:SetAttribute("Moneys",0)
			player.Character:MoveTo(randomPart.Position)
			player:SetAttribute("Role","Innocent")
		end
	end

Try doing this:

local teleportedPlayers = 0
local playersToTeleport = game.Players:GetPlayers()
for _, player in pairs(playersToTeleport) do
	task.spawn(function()
		if player.Character and player:GetAttribute("Trading") ~= nil then
			local randomPart = spawns:GetChildren()[math.random(1, #spawns:GetChildren())]
			player:SetAttribute("Alive",true)
			player:SetAttribute("Moneys",0)
			player.Character.PrimaryPart.Anchored = true
			repeat
				player.Character:PivotTo(randomPart.CFrame)
				task.wait(0.5)
			until (player.Character.PrimaryPart.Position - randomPart.Position).Magnitude <= 10
			teleportedPlayers += 1
			player:SetAttribute("Role","Innocent")
		end
	end)
end

repeat
	task.wait(0.5)
until teleportedPlayers >= #playersToTeleport

for _, player in pairs(playersToTeleport) do
	player.Character.PrimaryPart.Anchored = false
end

First I’m anchoring all of the teleporting players to get the magnitude part working without fail. I’m then looping the teleport multiple times until the character has actually teleported to fix lagging. After the player has been teleported the teleportedPlayers value counts up. I’m then waiting for all of the players to be teleported and later unanchoring them.

task.spawn() lets you run multiple things at the same time, I’m using this because you would have to wait for the first player to be teleported until the next one can. This helps with teleporting speed.

Let me know if you need any help!

2 Likes