Problems with rotating turret

Hello, I have a couple scripts which rotate a turret, but for some reason it stopped working and I have no idea why.

LocalScript:

wait(0.3)
local model = game.Workspace.AmericanBase.Cannons["Anti-Air1"]
local SG = script.Parent.UI
local value = SG.BulletsLeft
local label = SG.TextLabel
local mesh = model.MeshPart
local seat = model.Seat
local player = game.Players.LocalPlayer
local event = model.Rotate
local value = model.Touching

-----------------------------------------------------------------------------

print(player.Name)
local mouse = player:GetMouse()
print("Got the mouse")
while wait(0.001) do
	if seat.Occupant ~= nil then
		mouse.Icon = "rbxassetid://5030106330"
		local pos = mouse.Hit.Position
		if value.Value == true then
			event:FireServer(pos)
		end
	else
		mouse.Icon = ""
	end
end

ServerScript:

local event = script.Parent.Rotate
local mesh = script.Parent.MeshPart

event.OnServerEvent:Connect(function(plr, pos)
	mesh.CFrame = CFrame.new(mesh.Position, pos)
end)

The #2ND argument of CFrame is now deprecated… I suggest using CFrame.fromMatrix(), here is a nice tutorial on how to use it

2 Likes

Thank you for linking that. I’ve learned that this new method has some weird issues with edge cases, so I’ll also make a tutorial on that afterwards!

2 Likes

When did it get deprecated?

30chars

Did this but doesnt work

local event = script.Parent.Rotate
local mesh = script.Parent.MeshPart

event.OnServerEvent:Connect(function(plr, pos)
	mesh.CFrame = CFrame.fromMatrix(mesh.Position, 0, 0, pos)
end)

Sorry if I am doing funny stuff with it, its like REALLY hard to understand math stuff when they are not explained in your main language.

That’s not how you use the method you have to find the right and up vectors and supply them to the function.

local Direction = (pos - mesh.Position).Unit --assuming you want it to face pos
local RightVector = Direction:Cross(Vector3.FromNormalId(Enum.NormalId.Top))
local UpVector = RightVector:Cross(Direction)

mesh.CFrame = CFrame.new(mesh.Position, RightVector, UpVector)
1 Like

Tried this but it is not working

local event = script.Parent.Rotate
local mesh = script.Parent.MeshPart

event.OnServerEvent:Connect(function(plr, pos)
	local Direction = (pos - mesh.Position).Unit 
	local RightVector = Direction:Cross(Vector3.FromNormalId(Enum.NormalId.Top))
	local UpVector = RightVector:Cross(Direction)

	mesh.CFrame = CFrame.new(mesh.Position, RightVector, UpVector)
end)