Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Hello! I need some help, with this script, I don't know why it's not working? It's very hard.

Asked by 4 years ago
Edited by raid6n 4 years ago

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)

4 answers

Log in to vote
0
Answered by 4 years ago

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
Ad
Log in to vote
0
Answered by
zane21225 243 Moderation Voter
4 years ago

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)
0
I know this post is old but I'd like to point out that that is not the devforum. WideSteal321 773 — 4y
Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago

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)
Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago
-- 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)

Answer this question