How can I fix teleport script

So I have this problem when people with bad connection or something try to be teleport somewhere else which causes the whole game script to die is there a way I could skip the person aka not teleport them if their body part isn’t found in time?

for i,v in pairs(game.Players:GetChildren())do
			if v.TeamColor == BrickColor.new("Bright red") and v.TeamColor == BrickColor.new("Bright blue") then
  				local spawns = mapstorage["Map"..MapNum]:WaitForChild('Spawns'):GetChildren()
 				local spawne = math.random(1, #spawns)
 				local randomspawn = spawns[spawne]
				wait(1)
				if v.Character == nil then
					v.Character:WaitForChild("UpperTorso").CFrame = CFrame.new(randomspawn.Position + Vector3.new(0,2,0))
				else
					v.Character:WaitForChild("UpperTorso").CFrame = CFrame.new(randomspawn.Position + Vector3.new(0,2,0))
				end
			end
		end
		end

In your code it looks like you are still trying to teleport them even if their character isn’t found.

You should also use ipairs() and the game.Players:GetPlayers() function as it ensures you only get the player objects.

yes but how can I skip the player if the upper torso isn’t not found in ime

if (v.Character ~= nil) then
	local torso = v.Character:FindFirstChild("HumanoidRootPart")
	if (torso ~= nil) then
		torso.CFrame = CFrame.new(randomspawn.Position + Vector3.new(0,2,0))
	end
end

A part named ‘HumanoidRootPart’ should exist in both R15 and R6 characters, and also acts as the character model’s PrimaryPart.

1 Like