Make a part move and Rotate to Mouse.Position

Im working on a Deployable Robot that follows the players Mouse.Position and Turns to it.
I made the deploy and the Camera work.

  • How to make a part constantly turn and follow the PlayersMouse (Server Sidedly)

Here is my Server Script:

local clone = script.Parent.Remote
local model = nil
clone.OnServerEvent:Connect(function(p)
	local robot_model2 = game.ServerStorage.Robot:Clone()
	robot_model2.Parent = workspace
	robot_model2.Position = script.Parent.Handle.Position + (Vector3.new(0,0,-3))
	robot_model2.OwnerBot.Name = p.Name.. "BotSTR"
	model = robot_model2
end)


local function controlW(p, position)
	
	print(position)


	
		
		

	

end


script.Parent.KEYW.OnServerInvoke = controlW
1 Like

you can use the constructor CFrame.lookAt and the property CFrame.LookVector
like…

robot_model2.PrimaryPart.CFrame = CFrame.lookAt( Position of the eye or other words, robot_model2.PrimaryPart.Position, Mouse.position or look at)

and use the vector to advance to Vector3 do
Robot_model2.PrimaryPart.CFrame += robot_model2.PrimaryPart.CFrame.LookVector * studs per sec to advance

Thank you, ill try that and keep you updated!

  • The Robot clone is a Part called Model
local targ_pos = Vector3.new(position,position,0)
	model.CFrame = CFrame.lookAt(model.Position, targ_pos)

This didn’t really work, maybe i did something wrong?

Edit: i needed to convert Vector2 into Vector3

1 Like

use mouse.hit.p directly
return a vector3

1 Like

Yes it worked pretty well, but for some reason every time i clicked, the Speed of the Robot increased

local clone = script.Parent.Remote
local model = nil
clone.OnServerEvent:Connect(function(p)
	local robot_model2 = game.ServerStorage.Robot:Clone()
	robot_model2.Parent = workspace
	robot_model2.Position = script.Parent.Handle.Position + (Vector3.new(0,0,-3))
	robot_model2.OwnerBot.Name = p.Name.. "BotSTR"
	model = robot_model2
	
end)


local function controlW(p, position, stat)
	print(stat)
	print(position)
	local ticc = 50
	if stat == "Start" then
	model.CFrame = CFrame.lookAt(model.Position, position)
    model.CFrame += model.CFrame.LookVector * 0.03

	elseif stat == "Stop" then
		
	end
		

	

end


script.Parent.KEYW.OnServerInvoke = controlW

Hello!

  • Sorry for my English, I use a translator*

Your task is very easy to solve.
Have you ever worked with CFrames?
CFrame is a data type that stores not only the position of an object, but also its rotation. Using them saves a lot of time:

Instead of
Part.Orientation=Orientation;Part.Position=Position
You can write
Part.CFrame=CFrame

To rotate the bot, I use BodyGyro with its СFrame parameter, which indicates the rotation of the object
To move the bot, I use the BodyPosition with its Position parameter, which denotes the point in space to which the object tends.

To find out the location of the mouse, I use the Mouse object with its Hit(СFrame) parameter, which stores the position of the mouse in 3D space.

Example

Server:

local ServerStorage = game:GetService('ServerStorage')
local Robot = ServerStorage:WaitForChild('Robot',15) or print('Я не нашел модель')
---
local MODES= {
	COME = 1,
	STOP = 2
}
local LIMITS = {
	RADIUS = 100
}
----Constants
local Create = {}
-----
function Create:Folder(parent,name)
	local folder = Instance.new('Folder',parent)
	folder.Name =name
	return folder
end
function Create:RemoteEvent(parent,name)
	local folder = Instance.new('RemoteEvent',parent)
	folder.Name = name
	return folder       
end
function Create:CloneModel(model,cframe,parent,name)
	local clone = model:Clone()
	clone.Name =name
	clone.Parent  =parent
	clone.CFrame = cframe
	clone:ClearAllChildren()
	return clone
end
function Create:BodyPosition(parent,name)
	local body_pos = Instance.new('BodyPosition',parent)
	body_pos.Name = name
	return body_pos
end
function Create:BodyGyro(parent,name)
	local body_pos = Instance.new('BodyGyro',parent)
	body_pos.Name = name
	return body_pos
end    
local Folder = {}
local Event = {}
function Folder.new(name,parent)
	if not(parent:FindFirstChild(name)) then
		return Create:Folder(parent,name)
	else
		return parent:FindFirstChild(name)
	end
end
function Event.new(name,parent)
	if not(parent:FindFirstChild(name)) then
		return Create:RemoteEvent(parent,name)
	else
		return parent:FindFirstChild(name)
	end
end
---Server
local RemoteEvents = Folder.new('RemoteEventFolderForRobots',game.ReplicatedStorage)
	local RobotCreator = {}
function RobotCreator.new(_CFrame,Player)
	local self = {}
	self.Character = Player.Character
	self.RobotFolder = Folder.new(Player.Name.."-Robots",workspace)
	self.RemoteEvent = Event.new(Player.Name..'-Event',RemoteEvents)
	self.Id = tostring(#self.RobotFolder:GetChildren())
	self.Model = Create:CloneModel(Robot,_CFrame,self.RobotFolder,'Robot-'..self.Id)
	self.Position ={Position =Vector3.new(0,0,0)}
	if self.Model:IsA('Model') then
		self.Position  =Create:BodyPosition(self.Model.PrimaryPart,'Position')
		self.Gyro = Create:BodyGyro(self.Model.PrimaryPart,'Gyro')
		self.Model:SetPrimaryPartCFrame(_CFrame)
	else
		self.Position  =Create:BodyPosition(self.Model,'Position')
		self.Gyro = Create:BodyGyro(self.Model,'Gyro')
		self.Model.CFrame = _CFrame
	end
	---Functions 
	function self:GetPosition()
		if self.Model:IsA("Model") then
			return self.Model.PrimaryPart.Position
		else
			return self.Model.Position
		end
	end
	function self:Move(Position)
		if self.Position and self.Gyro then
			self.Position.Position = Position
			self.Gyro.CFrame =CFrame.new(self:GetPosition(),Position)
		end
	end
	function self:Stop()
		if self then
			self.Position.Position = self:GetPosition()
		end	
	end
	setmetatable(self,{__tostring =function()
		return 'Robot-'..self.Id
	end,
	__call=function()
		self.Connection = self.RemoteEvent.OnServerEvent:Connect(function(plr,Mouse,Mode)
			if Mode == MODES.COME and self.Character then
				self:Move(Mouse)
			end	
		end)
	end})
	return self
end
wait(10)
RobotCreator.new(CFrame.new(0,0,0),game.Players:GetChildren()[1])()

Client:

local MODES= {

COME = 1,

STOP = 2

}

local RemoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEventFolderForRobots"):WaitForChild(game.Players.LocalPlayer.Name.."-Event")

local Mouse = game.Players.LocalPlayer:GetMouse()

local InputService = game:GetService('UserInputService')

InputService.InputBegan:Connect(function(key)

if key.UserInputType == Enum.UserInputType.MouseButton1 then

RemoteEvent:FireServer(Mouse.Hit.Position,MODES.COME)

end

end)

robloxapp-20210327-1732230.wmv (739.8 KB)

4 Likes

you’ve have a bucle in local script?

Спасибо тебе за твой Ответ, я тоже русский, я все скрипты поставил как надо но почемуто не работает. И да, я еше новичок в CFrames.

Translation: Thanks for your reply, i am russian too, i adapted all the scripts but for some reason it did not work. And yes, im pretty new to CFrames

2 Likes

I have a Repeat Until in the Server Script

1 Like

Хм… Странно,у меня все работает, в начале есть задержка около 10 секунд… Может есть какие-то ошибки в консоле? Или может ты расположил LocalScript не в папке StarterCharactersScripts?(

2 Likes

Хммм, ошибка моя, все работает, я забыл Локальный в Tool

Спасибо Тебе большое!

2 Likes