Move a part to to wherever the mouse is clicked

Hello, I am trying to figure out how to click on a part to select it, then click again to move it to wherever the the mouse is clicked. I know almost nothing about scripting and would like to be pointed in the right direction and some explanation for what needs to be done, thank you very much for any help

2 Likes

Look into remote events and the following doc:

Once you detect a click, you can grab the mouse position (Mouse.Hit.Position) and fire that position to the server, and move the part from there. However, depending on what you trying to do, you might have to add sanity checks to your events to make sure exploiters don’t abuse them.

2 Likes

I’ve been looking at these the past couple days, and just don’t understand how I would put this into script at all

After some more looking at other scripts I notice most of them have a local script and a server script, if anyone could explain the whole process to me that would be very helpful thank you

Put simply, a remote event sends a message from a server script to a local script and vice versa.

(LocalScript, StarterCharacterScripts)

local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
    game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit.Position) --Vector3
end)

(ServerScript, ServerScriptService)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player,pos) --player is just a placeholder
    local part = Instance.new("Part",workspace)
    part.Position = pos --puts the part at the mouse's position
end)

Note that this script creates a new part, and puts it at the position of the mouse. If you don’t want to do that and just want an existing part to move, you can remove the local part = Instance.new("Part",workspace) and replace it with an already existing part.

1 Like

Thanks this has really helped. If I wanted to tween the part to each position would that need to happen inside the server script? I think I can work everything out from here, thanks again for the help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.