So i have a script, and it's not working, sometimes it works, and sometimes it just crashes your game! Please help me, it's for a game I'm working on. Here's the script.
local TeleportService = game:GetService("TeleportService") local gameID = {Put game link here} function onTouched(hit) local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then TeleportService:Teleport(gameID, player) end end script.Parent.Touched:connect(onTouched)
Defining the variable before checking if it has a player will cause an error. Instead of that, do this.
function OnTouched(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) then local Player = game.Players:GetPlayerFromCharacter(hit.Parent) then teleport lol end end
I've never had any experience w/ TeleportService, but the code I've written should work for you!
I read the official DevForum post on TeleportService and found out some things!
If you'd like to teleport the player to a game attached to your game, you can use the :Teleport()
function
Otherwise, use :TeleportToPlaceInstance()
.
Here is your fixed code, please accept this answer if it helps & let me know if the code I provided doesn't work.
-- Variables: local TeleportService = game:GetService('TeleportService') local gameId = 1010101 local Part = script.Parent -- Teleport Function: local function teleportPlr(Plr) wait() TeleportService:Teleport(gameId, Plr) end -- onTouched: Part.Touched:Connect(function(touched) if touched.Parent:FindFirstChild('Humanoid') then local Plr = game.Players:GetPlayerFromCharacter(touched.Parent) teleportPlr(Plr) end end)
Ahahahah, I've seen this problem before. We all hate roblox physics, dont we?
To fix this, youll have to add a debounce to your script.
local TeleportService = game:GetService("TeleportService") local gameID = {Put game link here} local debounce = false function onTouched(hit) if debounce == false then debounce = true local player = game.Players:GetPlayerFromCharacter(hit.Parent) if player then TeleportService:Teleport(gameID, player) wait(5) debounce = false end end end script.Parent.Touched:connect(onTouched)
-- Do not copy from here, click view source to get the properly indexed code. -- I think you were trying to teleport the player's character, not the player itself. local TeleportService = game:GetService("TeleportService") local GameId = 123456789 -- Put your GameId here. script.Parent.Touched:Connect(function(hit) if game.Players:GetPlayerFromCharacter(hit.Parent) and hit.Parent:FindFirstChildWhichIsA("Humanoid") then TeleportService:Teleport(GameId, game.Players:GetPlayerFromCharacter(hit.Parent)) end end)