Click Part to be Teleported

Hey, I’m trying to code a click teleport script, what I have currently teleports everyone in the server. I’m unsure how to modify it to only teleport the player who clicked it… This is my current script:

function onClicked()

local p = game.Players:GetChildren()

for i = 1, #p do

p[i].Character:MoveTo(Vector3.new(0.041, 1925.918, 9736.038))

end

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
3 Likes

Create an if statement saying if the player has clicked it, if not then the player can’t teleport.

How exactly would I do this? Making an if statement to check that is not my thing as I’m not good at coding.

Put something in the parameters to know which player that clicked it.

function onClicked(player)

    player.Character:MoveTo(Vector3.new(0.041, 1925.918, 9736.038))

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

MouseClick shows that it returns a player parameter. This means we can target who clicked and teleport only their character.

function onClicked(player)
    if player.Character then
        player.Character:MoveTo(Vector3.new(0.041, 1925.918, 9736.038))
    end
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)
2 Likes

I just now noticed it literally makes the player walk to that vector, how do I make it teleport?

Humanoid:MoveTo() makes the character walk to the position. Model:MoveTo() teleports the model to the position. As the character is a model, using Character:MoveTo() was correct.

2 Likes

Ah, I did not know that. I’ll edit my reply.

Alright it seems to be working.

Thanks so much for all your help! :smiley: