Button is not teleporting player after the player dies and respawns

I am making a admin panel, and in the admin panel there are buttons that let you teleport places. This worked for a little while but I was testing it one day, and it didn’t work correctly after the player died and respawned.

Local Script

local player = game.Players.LocalPlayer

local character = player.Character

local PawnshopTPLocation = game.Workspace:WaitForChild("PawnshopTPLocation")

script.Parent.MouseButton1Click:Connect(function()

character.Head.CFrame = CFrame.new(PawnshopTPLocation.Position)

end)

Is this script stored in StarterCharacter or StarterPlayer?..

If the latter - the reference to the character will need updating when the player respawns.

Try moving the character reference into the MouseClick function.

This is in a button in a GUI. Sorry for the confusion :sweat_smile:

I suggest then sticking a print function in the MouseClick, to see what is happening to the character and PawshopTPLocation.

If it is not printing, then the WaitForChild may not be indefinitely looking for the location part.

1 Like

I added a print after the variables, and near the teleport. The teleport prints but the variable print does not print.

I’ve tested your code in studio and in a live game and it works. Do you have ResetOnSpawn set to true on the ScreenGui as that could be causing the issue?

1 Like

Let me know if this does not work:


local PawnshopTPLocation = game.Workspace:WaitForChild("PawnshopTPLocation")

script.Parent.MouseButton1Click:Connect(function()

  script.Parent.Parent.Parent.Parent.Character.HumanoidRootPart.CFrame = CFrame.new(PawnshopTPLocation.Position)


end)

Yes I did have ResetOnSpawn set to false because I didn’t want admins to have to keep clicking it.

This probably won’t help but you should use:

game:GetService("Workspace")

instead of

game.Workspace

And, quoting the wiki:

This class is a Service! It is a top-level singleton which can be retrieved using the GetService function.

Just something to help you out in the future.

I hope you find your solution!!

1 Like

This still does not seem to work but I am pretty sure I just fixed it. But thanks! :smiley:

Hmm, that’s strange; good to hear you fixed it but I seemed to get no errors and it worked for me when I was testing. Good luck with your panel! :slight_smile:

1 Like

A shorter version with the same equivalent would be:

workspace

For those who are curious, I fixed it by taking the variables outside of the MouseButton1Click and put them inside so that when the player respawns and when they click it runs through the variables again.

Like this:

script.Parent.MouseButton1Click:Connect(function()

------- Variables

local player = game.Players.LocalPlayer

local character = player.Character

local PawnshopTPLocation = game.Workspace:WaitForChild("PawnshopTPLocation")

------- Variables

end)
4 Likes