Character is not found when gui button clicked

i need help with solving this problem. i have changed the code many times and it still says the same error. basically, I am trying to get the player teleported to a certain part once they click on the GUI button but it is saying character is not found?

here is the script that is in the textbutton

local tppart = game.Workspace.Elevator.Teleports:WaitForChild("1")

script.Parent.MouseButton1Click:Connect(function(plr)
	local plrname = plr.Character
	local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
	if humanoidrootpart then
		humanoidrootpart.CFrame = CFrame.new(tppart.CFrame.p) + Vector3.new(0,0,0)
	end
end)

First replace the script with a localscript. Scripts dont work in startgui and that should fix it!


local tppart = game.Workspace.Elevator.Teleports:WaitForChild("1")

script.Parent.MouseClick:Connect(function(plr)
	local humanoidRootPart = plr.Character and plr.Character:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		humanoidRootPart.CFrame = CFrame.new(tppart.CFrame.p) + Vector3.new(0,0,0)
	end
end)

you’re not checking for the character and the hrp, try this maybe

edit: you also used mousebutton1click when it should be a clickdetector

if it’s erroring, it got far enough to run the script

i am using a textbutton, not a clickdetector

I don’t think textbuttons pass the player who clicked the button as an argument, you’ll need to define that

actually, i think i found the problem, and thanks man

Usually a gui is controlled from a local script. You can get the player character from anywhere in the script:

local player = game.Players.LocalPlayer

then you can get the character:

local character = player.Character or player.CharacterAdded:Wait()