Teleport Players script isn't working

Hello
I recently made a player teleportation script that after a few seconds it teleport the players to a certain place. I used to work maybe it was due to a roblox update. But this this the script anyways.

SCRIPT

function onTouch(hit)
	if hit.Parent:FindFirstChild("NPC") then
		wait(10)
		local TeleportPoint = CFrame.new(24.85, 5.898, 117.04)
		for i, player in ipairs(game.Players:GetChildren()) do
			if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
				player.Character.HumanoidRootPart.CFrame = TeleportPoint + Vector3.new(0, i * 5, 0)
			end
		end
	end
end
script.Parent.Touched:Connect(onTouch)

Help appreciated! :smiley:

1 Like

Hi! when moving players you should either use the MoveTo or SetPrimaryPartCFrame function, here is how it can be used in your scenario:

function onTouch(hit)
	if hit.Parent:FindFirstChild("NPC") then
		wait(10)
		local TeleportPoint = CFrame.new(24.85, 5.898, 117.04)
		for i, player in ipairs(game.Players:GetChildren()) do
			if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
				player.Character:MoveTo(TeleportPoint + Vector3.new(0, i * 5, 0))
			end
		end
	end
end
script.Parent.Touched:Connect(onTouch)
1 Like

Just wondering, how come you’re looking for an “NPC” in line 2? Generally, you’d check if there is a Humanoid object.

When the NPC touches the part players get teleported

I don’t see much visible errors, but the one thing I would say is to make sure to use:

game.Players:GetPlayers()

This way, this will only get the players in case anything was inserted into the Players object.

1 Like
function onTouch(hit)
	if hit.Parent:FindFirstChild("NPC") then
		wait(10)
		local TeleportPoint = CFrame.new(24.85, 5.898, 117.04)
		for i, player in ipairs(game.Players:GetChildren()) do
			if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
				player.Character:MoveTo(TeleportPoint + Vector3.new(0, i * 5, 0))
			end
		end
	end
end
script.Parent.Touched:Connect(onTouch)

What @RomeoEDD has said here accurately displays how your script should look but the one thing I wanted to add is that you should be checking if it’s an actual Character because depending on accessories that the NPC is wearing this script could be void.

1 Like
function onTouch(hit)
	if hit.Parent:FindFirstChild("NPC") then
		wait(10)
		local TeleportPoint = CFrame.new(24.85, 5.898, 117.04)
		hit.Parent:MoveTo(TeleportPoint.P)
	end
end
script.Parent.Touched:Connect(onTouch)

What I have removed: Removed the for loop

What I have added: A simple statement, gets the parent of the hit object (The NPC/Player model), and calling the :MoveTo() function on it, which is an in-built function of ALL models (not just player ones, useful, innit?). This simply moves the entire model to the CFrame’s position coordinate (.P).

Alternatively, you could simply replace the CFrame with a Vector3 and remove the .P

1 Like