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

01local tp = script.Parent
02 
03tp.MouseButton1Click:Connect(function(go)
04local TeleportService = game:GetService("TeleportService")
05local lobby = 4920256514
06    local player = game.Players:GetPlayerFromCharacter(go.Parent)
07    if player then
08        TeleportService:Teleport(lobby, player)
09end
10end)
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

1local tp = script.Parent
2local TeleportService = game:GetService("TeleportService")
3 
4tp.MouseButton1Click:Connect(function()
5local lobby = 4920256514
6 TeleportService:Teleport(lobby)
7end
8end)

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.

01local player = game:GetService( "Players").LocalPlayer
02local tp = script.Parent
03local lobby = 4920256514
04local TeleportService = game:GetService("TeleportService")
05tp.MouseButton1Click:Connect(function(go)
06    if game.Players:GetPlayerFromCharacter(go) then
07 if player then
08        TeleportService:Teleport(lobby)
09    end
10end
11end

Answer this question