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)