I need a button that teleports player when clicked but the output says "attempt to index nil with parent" pls help
01 | local tp = script.Parent |
02 |
03 | tp.MouseButton 1 Click:Connect( function (go) |
04 | local TeleportService = game:GetService( "TeleportService" ) |
05 | local lobby = 4920256514 |
06 | local player = game.Players:GetPlayerFromCharacter(go.Parent) |
07 | if player then |
08 | TeleportService:Teleport(lobby, player) |
09 | end |
10 | end ) |
Try putting this in a LocalScript
1 | local tp = script.Parent |
2 | local TeleportService = game:GetService( "TeleportService" ) |
3 |
4 | tp.MouseButton 1 Click:Connect( function () |
5 | local lobby = 4920256514 |
6 | TeleportService:Teleport(lobby) |
7 | end |
8 | end ) |
The player will default to LocalPlayer 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.
01 | local player = game:GetService( "Players" ).LocalPlayer |
02 | local tp = script.Parent |
03 | local lobby = 4920256514 |
04 | local TeleportService = game:GetService( "TeleportService" ) |
05 | tp.MouseButton 1 Click:Connect( function (go) |
06 | if game.Players:GetPlayerFromCharacter(go) then |
07 | if player then |
08 | TeleportService:Teleport(lobby) |
09 | end |
10 | end |
11 | end |