I am making a random place finder, and I have a function to teleport to the game. There are 2 effects to fire when the teleport works, and when the teleport doesn’t.
Yes, I have tried this in-game outside of studio.
This script is a remote event in a GUI.
Keep in mind sometimes the ID leads to a game that isn’t public, that’s what the “teleport didn’t work” effect is for.
Sometimes the “teleport worked” effect appears, even when the teleport doesn’t work.
I have tried pcall, and haven’t found a way to properly make it work.
local t = true --can teleport
local ts = game:GetService("TeleportService") --teleport service
script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr)
if t == true then
t = false
ts:Teleport(game.Lighting.CoreGameID.Value,plr)
local teleport = true --determines the effect
ts.TeleportInitFailed:Connect(function(plre, teleportResult, errorMessage) --detects when error occurs
if plre.UserId == plr.UserId then --make sure player is our player
teleport = false --sets to false for effect
end
end)
wait(.5) --I gotta add this or the code always returns true for some reason
if teleport == false then
local aaa = script.Parent.TpfailS:Clone() --warning text label
aaa.Parent = script.Parent.Parent
aaa.Visible = true
local strings = plr.Name.." is teleporting!"
game.Debris:AddItem(aaa,2)
elseif teleport == true then
local caa = game.Lighting.Part.Attachment:Clone() --teleport effect
game.Debris:AddItem(caa,5)
caa.Steal:Play()
caa.Parent = plr.Character.PrimaryPart
end
wait(2)
t = true --enable ability to teleport again
end
end)