Problem with GUI Teleport

Hi, I am trying to make a GUI element teleport a player and I don’t really understand why it’s failing

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:connect(function()
	local hrp = player.Character.HumanoidRootPart
	hrp.CFrame = game.Workspace.teleport.CFrame

end)

When the button is pressed, the console gives an error, “attempt to index character with nil”
I am sure the solution is quite obvious lol, I just can’t figure out how to get it working

1 Like

try a :WaitForChild on humanoidrootpart

local hrp = player.Character:WaitForChild("HumanoidRootPart")

The character is the problem, not the HRP. I did try a WaitForChild and it said attempt to index nil with “WaitForChild”

is the code you sent the full code or not

Is your character actually loading? Or is CharacterAutoLoads off?

Also, I recommend saying

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
script.Parent.MouseButton1Down:connect(function()
	hrp.CFrame = game.Workspace.teleport.CFrame
end)

It is loading I believe, I’ll try that script

Attempt to index nil with ‘Character’ :confused:

Are you using a LocalScript or a ServerScript?

I did test and if you get LocalPlayer on Server it returns nil with no error, then if you try to get the character it, then proceeds to error.

1 Like

ServerScript inside of the ImageButton

Well, that is the issue. You can not get the LocalPlayer on the Server.

If you want to get the player, then you will have to go from the Script and go from Parent to Parent until you get the Player.

You can change the Script to a LocalScript and it would still teleport the Character.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.