Turret attached to vehicle

I need help with a turret attached to a vehicle, for some reason when I drive off, the turret stays in its position, its attached to the vehicle with a hinge constraint.

Here’s a video for reference:

Here’s the code for making the turret follow the mouse position:

Remote.OnServerEvent:Connect(function(player, mouse)
	
	local TurretPos = Turret.Position
	Turret.CFrame = CFrame.lookAt(TurretPos, Vector3.new(mouse.X, TurretPos.Y, mouse.Z))
	
end)

I’m firing this remote from a local script every second the player is sitting on the vehicle seat.

Any help appreciated!

What is the server perspective when you switch to server view?

I believe there is a problem if you manually edit CFrame with hinge constraints like the one below:

This is an example of calculating the servo angle assuming the turret base is the Attach0 and the turret part is attach1.

When I switch to the server it ‘updates’ to the right place on top of the vehicle, its hard to explain this, see the video below for reference:

Notice the attachments are trying to pull back together.

I recommend using the other method I suggested in the post above with Servo angles, I believe the issue is because of the physics disrepency between server and client because it’s being CFramed on the server but the physics is on the client which usually is the case for vehicles.

Or you could use a weld or Motor6D approach and change the C0 if you want to go with a CFrame approach.

Could you explain this in depth?

Perhaps try to use a runservice heartbeat loop with the CFraming

local mouse = Vector3.new()
RunService.Heartbeat:Connect(function()
local TurretPos = Turret.Position
	Turret.CFrame = CFrame.lookAt(TurretPos, Vector3.new(mouse.X, TurretPos.Y, mouse.Z))
end)


--asd on server event to update mouse variable

If it doesn’t work try using .Stepped event, perhaps it’s the order of the task scheduler in which the physics work and when the turret position gets updated together with the physics.

Hard to test this out and explain further of the issue in depth without a repro file.

The issue with CFraming the turret on the server, is the fact that the client has network ownership of the car.

By creating a car clone on the server it is easy to see what the server see which is the car behind the players car lagging behind due to well Physics and network lag.

Consequently, when you CFrame the turret on the server the turret will go to the Server position of the turret which is behind the vehicle which will pull the clients car backwards. Moreover when you run this every RenderStepped as often as possible, yeah the turret will remain in place.

I recommend cloning a local script into the client PlayerGui, and have that local script change the servo angle instead to avoid this Network issue.

Script for the car clone:


local RunService = game:GetService("RunService")
local Turret = script.Parent.CannonHeadPart
local turretModel = script.Parent
local cannon = script.Parent.lidPart
local Remote = script.Parent.Remote
local newMouse = Vector3.new()

Remote.OnServerEvent:Connect(function(player, mouse)
	print(mouse)
	newMouse = mouse
		--local TurretPos = Turret.Position
		--Turret.CFrame = CFrame.lookAt(TurretPos, Vector3.new(mouse.X, TurretPos.Y, mouse.Z))

end)


local carClone = script.Parent.Parent:Clone()
for i,v : BasePart in pairs(carClone:GetDescendants()) do
	if v:IsA"BasePart" then
		v.CanCollide = false
		v.Anchored = true
		v.CanQuery = false
		v.Transparency = 0.5
	end
	if v:IsA("Script") then
		v:Destroy()
	end
end
carClone.Parent = workspace
RunService.Heartbeat:Connect(function()
	carClone:SetPrimaryPartCFrame(script.Parent.Parent:GetPivot())	
end)
while task.wait(1) do	
	local TurretPos = Turret.Position
	Turret.CFrame = CFrame.lookAt(TurretPos, Vector3.new(newMouse.X, TurretPos.Y, newMouse.Z))
	--turretModel:PivotTo(CFrame.lookAt(TurretPos, Vector3.new(newMouse.X, TurretPos.Y, newMouse.Z)))
end
1 Like

But that would mean only the player driving the vehicle would be able to see the turret rotate, right?

Nope, the player has network ownership of the vehicle. Since the turret is physics operated with a servo motor on client it will be replicated.

It’s one of the exceptions to the client server rule

While part properties do not technically replicate, there are a few exceptions. If the part is created by a client and that client simulates physics (the part falls, hits other objects, moves by constraints, etc.), the resulting changes will get copied to the server.

1 Like