How do I change the players place on click?

Hello I am trying to make a brick that changes the players place when it is clicked on. Here is what I have so far.

function onClicked(hit)
   local player = game.Players:GetPlayerFromCharacter(hit.Parent)
   if player then
		game:GetService("TeleportService"):Teleport(5710227729,player) 
  end
end


script.Parent.Touched:connect(onClicked)

I am unsure of why this isn’t working. There isn’t even an error output that I can see.

1 Like

Are you running it on a live server? TeleportService will not run in Studio.

I know it won’t run in studio. I have another script that runs when the button is stepped on and it outputs an error saying it cant run in studio. This one dosen’t output anything

you can separate the :GetService and the :Teleport like :

local TeleportService = game:GetService("TeleportService")

then you can do :

TeleportService:Teleport(5710227729,player)

(also TeleportService does not work in the studio.)

Just out of interest, you named your function onClicked however you use the .Touched event and a hit argument. Is this definitely the right method, are players touching something or clicking something?

they are clicking something not touching

gui or part click?
if its a part then use a clickdetector
elseif its a gui then mousebutton1click:connect(function()

I already have a click detector in the part. I feel like im missing something obvious some how.

If you already have the clickdetector then why are you using .Touched on it? You should do

ClickDetector.MouseClick:Connect(function(player)
        -- Teleport here
end)

to teleport the player when you click on the part.

function onClicked(player)
	if player then
		game:GetService("TeleportService"):Teleport(5710227729,player) 
	end
end


script.Parent.MouseClick:Connect(onClicked)
2 Likes

In which case use the MouseClick function as shown here with a player argument, no need to get player from character just teleport the player from the argument.

This is presuming that script.Parent is of course a Click Detector.

function onClicked(player)
  game:GetService("TeleportService"):Teleport(5710227729,player)
end


script.Parent.MouseClick:connect(onClicked)

for some reason its still not outputting a error message saying it cant be ran in studio. I even changed the “MouseClick” to “ClickDetector” (as thats what mine is called) and it still has no output

Can you screenshot the path please to the script and the click detector? Changing it to ClickDetector will not work as that’s the function which is registers as a click for. Make sure the detector is actually being clicked.

Capture

function onClicked(player)
  game:GetService("TeleportService"):Teleport(5710227729,player)
end


script.Parent.MouseClick.MouseClick:Connect(onClicked)

That all being said, the original script should be erroring since the path is invalid.

Having you now shown this, it would appear that you need to rename your ClickDetector back to something else so it doesn’t think you’re trying to MouseClick the brick, rename it back to ClickDetector and use the following code!

function onClicked(player)
  game:GetService("TeleportService"):Teleport(5710227729,player)
end


script.Parent.ClickDetector.MouseClick:Connect(onClicked)```