How do I obtain HumanoidRootPart from a GUI Clicked event?

Hi all,
I know this is probably a simple solution, but I’m new and I cannot figure out a solution to this problem: I have a piece of code that, when clicked on, should teleport the player to a random area. I have an issue with getting the player’s HumanoidRootPart, as using LocalPlayer doesn’t seem to work, and using a normal script simply returns “nill”.

Here’s my code (no teleporting code as of right now, but I know how to do it, I just want to figure out why I can’t get the HumanoidRootPart first.):

local button = script.Parent

local function teleport(player)
	local humanoid = game:GetService("Players").LocalPlayer:FindFirstChild("HumanoidRootPart")
	if humanoid then
		print("it works")
	end
	
end

button.MouseButton1Click:Connect(teleport)

As mentioned previously, i’ve tried getting the HumanoidRootPart through LocalPlayer without success, and a normal script doesn’t seem to work as well (player variable returns nill).

Thank you!

1 Like
local player = game:GetService("Players")
local char = player.Character or player.CharacterAdded:Wait()

local humanoidRootPart = char:WaitForChild("HumanoidRootPart")
local destination = CFrame.new(0,0,0)

humanoidRootPart.CFrame = destination

This needs to be in a localscript of course, if you have any further questions feel free to ask.

Well, the thing is, the humanoidRootPart is in the character, not the player, meaning you’d have to do

LocalPlayer.Character:FindFirstChild("HumanoidRootPart")