(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.
(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.
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)
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.
--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).