Guidance on how to achieve a part movement system

This is only for me to learn from and challenge myself.

So basically I am trying to have a system to move models fairly similar to the one seen in The Conquerers 3. The idea is to have the player click and get the mouse position and use it to move a model to that position. I had no idea how to do this other than getting the players mouse position using Mouse.Hit.p. I did some quick googling and found someone suggested using BodyPosition and BodyGryo inside a part. So after reading about them I did some experimenting in Roblox Studio to see what they could do. I ended up coming up with this quick script to move a part with a BodyPosition inside it to the player’s mouse position when they clicked.

The quick script(s) I made

Local Script inside StarterPlayerScripts to get the mouse position when player clicks

-- Local Script
local RS = game:GetService("ReplicatedStorage")
local MouseHitData = RS:WaitForChild("MouseHitData") --Remote event to send to server script

local localPlayer = game:GetService("Players").LocalPlayer
local mouse = localPlayer:GetMouse()

local unit = game.Workspace.Unit -- The part I am using
local BodyPosition = unit.BodyPosition --The BodyPosition inside the part

mouse.Button1Down:Connect(function() -- function to send the mouse position to the server when left clicked
	local MousePos = mouse.Hit.p
	MouseHitData:FireServer(MousePos)
end)

Server Script to actually move the part to the mouse position

-- Server Script
local RS = game:GetService("ReplicatedStorage")
local MouseHitData = RS:WaitForChild("MouseHitData") --Remote event to send to server script

local unit = game.Workspace.Unit
local BodyPosition = unit.BodyPosition
local BodyGyro = unit.BodyGyro

MouseHitData.OnServerEvent:Connect(function(player, NewPosition) -- Remote event from local script
	print("Recieved:", NewPosition) -- checking I recieved the mouse position
	BodyPosition.Position = NewPosition -- moves the part to the mouse position
	BodyGyro.cframe = CFrame.new(NewPosition) -- I think this makes it orientate towards the mouse position not sure though
end)

So that’s what I achieved and found it did move the part to the player’s mouse position and I could alter the speed in which it does this by changing the force of the bodyPosition. However, since I’m still new to scripting and Roblox Studio I don’t know if this is the best or even correct way to achieve what I want. So what I am asking is if anyone who reads this has an idea of how I could achieve sort of the same movement system as in The Conquerors 3 and if anyone has any resources I can check out to help out with this challenge to myself.

Before anyone replies saying that you are not supposed to ask for people to give you the code to do this, I am not asking for the code for this to be handed to me. I am simply asking for guidance on how to achieve this and to improve as a scripter. and no I am not trying to rip off The Conquerors 3 I love the game and play it regularly I just simply want to try and code this myself.

Thanks for taking the time to read this.

3 Likes

If you wanted to place down a part, use mouse.Hit.p
If you wanted to move a part by clicking on it, use mouse.Target

2 Likes

That’s not exactly what I am asking, maybe I worded this wrong so sorry.
I am trying to find out the correct way to achieve part movement to the mouse position smoothly as if the part was actually travelling to the mouse position, not instantaneously. The idea is that the part would in the future be a unit such as a soldier and you would be able to move it around the map by clicking and setting a point for it to move to like in the conquerors 3.

3 Likes

If you’re going to be moving from one point to another in straight lines, you could use :lerp() and a for loop.

2 Likes

I never thought about that I’ll give it a go and see if it works how I want it to.
Thanks

1 Like