Teleport script inconsistently teleporting or working

(Please tell me if this is the right place to post this else I don’t know where to.)
Hey I made a script that can teleport a player to a other game/same game again and some times when I touch the part that have the script in it, it says failed to teleport to game/place, and when I touch it again it works and sometimes not. Please help! Heres the script:

– Made by YTWolf_Dk

local TeleportService = game:GetService(“TeleportService”)
local gameID = 4683491892

function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
TeleportService:Teleport(gameID, player)
end
end

script.Parent.Touched:connect(onTouched)

Please give this post some feedback because I am new on the devforum. :slight_smile:

Welcome to the DevForums!
This category is for reviewing code, but you can put your topic here: Scripting Support - Developer Forum | Roblox

(also, you should wrap your code snippets in code boxes by wrapping your message around ``` at the start and end)

You need to add checks for seeing if it’s a player or not. We can do it like this:

-- Made by YTWolf_Dk

local TeleportService = game:GetService(“TeleportService”)
local gameID = 4683491892

function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player then
			TeleportService:Teleport(gameID, player)
		end
	end
end

script.Parent.Touched:Connect(onTouched)

Also, when using .Touched(), it will keep repeating until the player stops touching the part. You should add a debounce or some way of disabling the player’s interaction with the part.

1 Like

Hey thanks for welcoming me. And also I am new to scripting so I don’t fully know all you said but I did get some of it.

No worries!
This is a way of doing a basic debounce (stopping the player from using it after already using it)

-- Made by YTWolf_Dk

local TeleportService = game:GetService(“TeleportService”)
local gameID = 4683491892
local debounce = false

function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player and debounce == false then
			debounce = true
			TeleportService:Teleport(gameID, player)
		end
	end
end

script.Parent.Touched:Connect(onTouched)
1 Like

So am I supposed to use the newest script (the one you typed) Does that fix the not teleporting eror?

I am also new to scripting and being a dev if anyone was wondering. :slight_smile:

It may, just try it. It sounds like it keeps trying to repeatedly teleport you, but if it’s already teleporting you then it’ll error. TeleportService takes a little while to teleport you, so that may be why it appears to not work at first.

This isn’t the way to comment things.

--Some comment that will not throw an error
– This text isn't a comment and script will error because this line cannot be executed

The :connect thing is deprecated. :Connect should be used instead.

And the “solution” mark should be given to the post that actually fixed your issue (in this case, to the post where @Polyheximal told you the working code).

2 Likes