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

How to make the button teleport player to lobby when clicked?

Asked by 4 years ago

I need a button that teleports player when clicked but the output says "attempt to index nil with parent" pls help

local tp = script.Parent

tp.MouseButton1Click:Connect(function(go)
local TeleportService = game:GetService("TeleportService")
local lobby = 4920256514
    local player = game.Players:GetPlayerFromCharacter(go.Parent)
    if player then
        TeleportService:Teleport(lobby, player)
end
end)
0
Are you using a part or a GUI? OhManXDXD 445 — 4y
0
no i have the button but it wont teleport player ot the lobby coolmanHDMI 72 — 4y
0
I know but are you clicking a GUI button or a Part? OhManXDXD 445 — 4y
0
if you are using a gui, by saying "go.Parent" you would be trying to get the Players folder. Change it to just "go" DarkDanny04 407 — 4y

2 answers

Log in to vote
1
Answered by
OhManXDXD 445 Moderation Voter
4 years ago
Edited 4 years ago

Try putting this in a LocalScript

local tp = script.Parent
local TeleportService = game:GetService("TeleportService")

tp.MouseButton1Click:Connect(function()
local lobby = 4920256514
 TeleportService:Teleport(lobby)
end
end)

The player will default to LocalPlayer in a LocalScript

0
sorry I had to remove player.. remember to put this in a LocalScript OhManXDXD 445 — 4y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

There is two errors I can see, first is that you reference the player that joined the game and your trying to index something that is nil on line 6.

local player = game:GetService( "Players").LocalPlayer 
local tp = script.Parent
local lobby = 4920256514
local TeleportService = game:GetService("TeleportService")
tp.MouseButton1Click:Connect(function(go)
    if game.Players:GetPlayerFromCharacter(go) then
 if player then
        TeleportService:Teleport(lobby)
    end
end
end

Answer this question