I want it so that when a player clicks a GUI button they are teleported to another place (from the lobby to the main game)
I keep on getting this error message:
Players.iiKhico.PlayerGui.ScreenGui.modeselectionforunidenifiedfairies.rpmode.LocalScript:5: attempt to index nil with ‘Parent’ - Client - LocalScript:5
I can’t seem to find anyone else with the same issue and all the youtube vids I find aren’t teleporting from place to place in the same game and the docs don’t make sense to me
local TeleportService = game:GetService("TeleportService")
local gameID = 16428583844
function leftclick ()
local player = game.Players:GetPlayerFromCharacter(hit.Parent) --line 5
if player then
TeleportService:Teleport(gameID, player)
end
end
script.Parent.MouseButton1Click:Connect(leftclick)
The reason why you’re getting that error is mainly because what you’re getting the “Parent” property of is nil or non-existent. In other words, it can’t get “Parent” if there’s nothing to even get “Parent” from. And if you were to look in your code:
function leftclick () -- this is missing the "hit" variable, which would be given if you're using a part.Touched event
local player = game.Players:GetPlayerFromCharacter(hit.Parent) -- hit would be nil, hence your error
if player then
TeleportService:Teleport(gameID, player)
end
end
But, if you want to get the player from the person who clicked on it, I think your best bet is to simply just get the local player from the Players service since you’re using a LocalScript.
Did you rip the left click function from a video by chance?
ok, so i changed the script from a regular script to local script, and added hit to the parenthesis next to left click but I’m still getting the same error. also I’m rlly sorry but Idk how to get the local player from the players service.
Pretty much what @Avvalor said, but then you wouldn’t have the need to check if there’s a player since you’ll be teleporting the local player to the game place.
Your full script would be this
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local gameID = 16428583844
function leftclick ()
TeleportService:Teleport(gameID, player)
end
script.Parent.MouseButton1Click:Connect(leftclick)