Turret control using mouse

Hey devs, how can I go about creating a turret controlled by the mouse?

Any help appreciated!

Does anyone know how I could do this?

If you want to make turret look at the direction of the place where the mouse is then you need to get mouse position and set model’s PrimaryPart’s CFrame to CFrame.new(Part.Position, MousePosition)

But that would make the turret instantly look at the mouse position, I want certain turrets to have higher rotate speeds and some slower.

prototype_turret.rbxl (46.7 KB)


I made working prototype
Also I am using cframe:Lerp(goal,alpha)

local turret =workspace.Model.turret
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local uis = game:GetService("UserInputService")
local doing = false
local speed = 0.01
uis.InputBegan:Connect(function(input,chatting)
	if chatting then
		return
	end
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local hitpos = mouse.Hit.Position
		local target_cf = CFrame.new(turret.Position, hitpos)
		local start_cf = turret.CFrame
		if doing == true then
			return
		end
		doing = true
		for i = 0,1,speed do
			turret.CFrame = start_cf:Lerp(target_cf,i)
			task.wait()
		end
		doing = false
		
	end
end)

But this thing works fully on client side so i think you need to send a remote with mouse position and then do this thing on server

I suppose you can also do is:

local turret =workspace.Model.turret
local plr = game:GetService("Players").LocalPlayer
local mouse = plr:GetMouse()
local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local responsiveness = 5
mouse.Move:Connect(function()
	if uis:IsMouseButtonDown(Enum.UserInputType.MouseButton1) then
		local hitpos = mouse.Hit.Position
		local target_cf = CFrame.new(turret.Position, hitpos)
		local start_cf = turret.CFrame
		turret.CFrame = start_cf:Lerp(target_cf,rs.RenderStepped:Wait()*responsiveness)
	end
end)

Would be more quicker then that.

Now to OP:
I would recommend you using remoteevents to turn the turret server wise because this is only local.