Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

4 comments

0

Are you using a part or a GUI?

0

no i have the button but it wont teleport player ot the lobby

0

I know but are you clicking a GUI button or a Part?

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"

2 answers

✓ Accepted answer

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

1 comment

0

sorry I had to remove player.. remember to put this in a LocalScript

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

This discussion is preserved as it was. New answers, comments, and votes are closed.