Trying to make tank turret rotate with mouse

Hello. I am trying to get my turret to rotate to wherever the players mouse is pointing. I’ve tried achieving it and sorta worked but sorta not.

Explorer:
image

Basically the script below technically works but it rotates the whole tank due to welds. Now at the start of the game the tank is welded in the Framework script to keep the tank from falling apart. But i’m guessing that these welds are making it so the whole tank is rotated when i move my mouse.

LocalScript (Tank.LocalScript)

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local TurretUnion = game.Workspace.M4Sherman.BodyKit.Turret.PrimaryPart

while wait() do
	TurretUnion.CFrame = CFrame.new(TurretUnion.Position, mouse.Hit.Position)
end

It’s hard to explain so here is a video of my problem.
robloxapp-20250119-0204318.wmv (1.3 MB)

And dw about the script inside the tool it doesnt do anything yet.

Any help is appreciated :smile:

2 Likes

This looks like a welding issue

if you want to have something stay in position but not weld it the best thing to do is probs attach a function to renderstepped via the run service.
This mean that at the start of every frame before it is drawn this function will run.

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local TurretUnion = game.Workspace.M4Sherman.BodyKit.Turret.PrimaryPart
local M4Sherman= game.Workspace.M4Sherman.PrimaryPart

local Connection = game:GetService("RunService").RenderStepped:Connect(function()
	TurretUnion.CFrame = CFrame.new(M4Sherman.Position+vector3.new(offsetx, offsety,offsetz), mouse.Hit.Position)
end)
-- then when you want this behaviour to stop
Connection:Disconnect()

I must misunderstand what you mean or done something wrong. Here is what happens :skull:

robloxapp-20250124-2303192.wmv (551.8 KB)

Here is the script:

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local TurretUnion = script.Tank.Value.BodyKit.Turret.PrimaryPart
local M4Sherman = script.Tank.Value.PrimaryPart
local tool = script.Parent

local Connection

tool.Equipped:Connect(function()
	Connection = game:GetService("RunService").RenderStepped:Connect(function()
		TurretUnion.CFrame = CFrame.new(M4Sherman.Position+Vector3.new(0, 0, 0), mouse.Hit.Position)
	end)
end)

tool.Unequipped:Connect(function()
	Connection:Disconnect()
end)

yeah you need to remove welds too

tbh you would probs be better off using roblox’s constraints system.
I think you want to use a hinge constraint, insted of welds.

a constraint is basically a weld but it allows for movement like rotation around an axis

Here is how i’ve welded the tank.


image

Do i unweld all parts inside the turret? Or do i just unweld the primpart?

umm you want all the body parts welded to the primary body part
you want all turret parts welded to the main turret part
then you want the turret part hinged to the body part

Okay now i’ve welded all the parts inside turret to the primpart and then i’ve made a hingeconstraint.

I’ve never used hingeconstraint before so I’m sorry for any mistakes i make. I’ve put attachment0 to the attachment in the turret primpart and attachment1 to an attachment in the mainpart.

@PotatoVampire7797
Hello. I am currently running what I have in my previous post but its still glitching weirdly the whole tank flyes under the map and its a weird situation. Can you help?

Yes but I cant do this without welds or the tank falls apart. Is there a workaround?

umm i can only guess you got attachemnt 0 and attachment 1 on your welds and constraints backwards somewhere… Im pretty sure this is what causes the randomly fly into the air physics bug. best to check Documentation for the correct way round to do it.