So when my car with a turret is in its start position, the turret rotates perfectly, but when i move my car, the turret rotation just breaks, the turret just doesn’t follow the mouse anymore and just rotates with offset.
Local script:
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Mouse = Player:GetMouse()
local Events = RS:WaitForChild("Events")
local ATGMValue = script:WaitForChild("ATGMValue")
local VehicleValue = script:WaitForChild("VehicleValue")
local ATGM = ATGMValue.Value
local MoveTurretEvent = Events:WaitForChild("MoveTurret")
local SitEvent = Events:WaitForChild("PlayerSitting")
local FireEvent = Events:WaitForChild("Fire")
local MousePosEvent = Events:WaitForChild("MousePos")
local Camera = workspace.CurrentCamera
local IsSitting = false
local IsAiming = false
local debounce = false
Mouse.TargetFilter = VehicleValue.Value
SitEvent.OnClientEvent:Connect(function(Occupant)
if Occupant ~= nil then
Humanoid:UnequipTools()
IsSitting = true
elseif Occupant == nil then
IsAiming = false
IsSitting = false
ExitCamera()
end
end)
Mouse.Button1Down:Connect(function()
if IsSitting == true and IsAiming == true then
FireEvent:FireServer()
end
end)
Mouse.WheelForward:Connect(function()
if IsAiming == true and Camera.FieldOfView > 15 then
Camera.FieldOfView = Camera.FieldOfView - 5
end
end)
Mouse.WheelBackward:Connect(function()
if IsAiming == true and Camera.FieldOfView < 60 then
Camera.FieldOfView = Camera.FieldOfView + 5
end
end)
UIS.InputBegan:Connect(function(input,gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.X and not gameProcessedEvent and IsSitting == true then
if IsAiming == false then
IsAiming = true
EnterCamera()
elseif IsAiming == true then
IsAiming = false
ExitCamera()
end
end
end)
function ExitCamera()
local CamPart = ATGM:WaitForChild("CamPart")
IsAiming = false
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = Humanoid
Camera.FieldOfView = 70
Player.CameraMode = Enum.CameraMode.Classic
UIS.MouseDeltaSensitivity = 1
UIS.MouseBehavior = Enum.MouseBehavior.Default
UIS.MouseIconEnabled = true
end
function EnterCamera()
local CamPart = ATGM:WaitForChild("CamPart")
IsAiming = true
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = CamPart
Camera.FieldOfView = 60
Player.CameraMode = Enum.CameraMode.LockFirstPerson
UIS.MouseDeltaSensitivity = 0.06
UIS.MouseIconEnabled = true
UIS.MouseBehavior = Enum.MouseBehavior.Default
end
local function MoveTurret()
if IsAiming == true then
local HHinge = ATGM:WaitForChild("HHinge")
local VHinge = ATGM:WaitForChild("VHinge")
local Delta1 = Mouse.Hit.Position - VHinge.CFrame.Position
local Delta2 = Mouse.Hit.Position - HHinge.CFrame.Position
local Angle1 = math.atan2(Delta1.Z,-Delta1.X)
local Angle2 = math.atan2(Delta2.Y,-Delta2.X)
print(math.deg(Angle1))
MoveTurretEvent:FireServer(Angle1,Angle2)
elseif IsAiming == false then
Camera.CameraType = Enum.CameraType.Custom
Camera.CameraSubject = Player.Character.Humanoid
end
end
RunService.RenderStepped:Connect(MoveTurret)
RunService.RenderStepped:Connect(MoveTurret)
Server script:
local PlayerSitting
local ATGM = script.Parent.Parent
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local Events = RS:WaitForChild("Events")
local PlayerSittingEvent = Events:WaitForChild("PlayerSitting")
local MoveTurretEvent = Events:WaitForChild("MoveTurret")
local FireEvent = Events:WaitForChild("Fire")
local Missile = ATGM:WaitForChild("Missile")
local FireSound = ATGM:WaitForChild("HRotate"):WaitForChild("Tube"):WaitForChild('FireSound')
local MissileValue = script.Parent:WaitForChild("Missile")
local GUI = script.Parent:WaitForChild("ATGMUI")
local VHinge = ATGM:WaitForChild("VHinge")
local HHinge = ATGM:WaitForChild("HHinge")
local debounce = false
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
if script.Parent.Occupant ~= nil then
PlayerSitting = script.Parent.Occupant.Parent
local Player = Players:GetPlayerFromCharacter(PlayerSitting)
VHinge.HingeConstraint.TargetAngle = 0
HHinge.HingeConstraint.TargetAngle = 0
if not Player:WaitForChild("PlayerGui"):FindFirstChild("ATGMUI") then
local GUIClone = GUI:Clone()
GUIClone.Parent = Player:WaitForChild("PlayerGui")
end
PlayerSittingEvent:FireClient(Player,script.Parent.Occupant)
elseif script.Parent.Occupant == nil then
local Player = Players:GetPlayerFromCharacter(PlayerSitting)
PlayerSittingEvent:FireClient(Player,script.Parent.Occupant)
MissileValue.Value = nil
if Player:WaitForChild("PlayerGui"):FindFirstChild("ATGMUI") then
Player:WaitForChild("PlayerGui"):FindFirstChild("ATGMUI"):Destroy()
end
end
end)
MoveTurretEvent.OnServerEvent:Connect(function(player,Angle1,Angle2)
VHinge.HingeConstraint.TargetAngle = math.deg(Angle1)
HHinge.HingeConstraint.TargetAngle = math.deg(Angle2)
end)
FireEvent.OnServerEvent:Connect(function(player)
if debounce == false and MissileValue.Value == nil then
debounce = true
Missile.Transparency = 1
FireSound:Play()
MissileFire()
task.wait(5)
debounce = false
end
end)
function MissileFire()
local MissileClone = Missile:Clone()
if MissileClone then
MissileClone.Transparency = 0
MissileClone.Parent = script.Parent.Parent.ATGMMissile
MissileValue.Value = MissileClone
MissileClone.CFrame = Missile.CFrame * CFrame.new(0,0,20)
MissileClone:WaitForChild("ControlScript").Enabled = true
MissileClone:WaitForChild("Fire").Enabled = true
MissileClone:WaitForChild("Flare").Enabled = true
MissileClone:WaitForChild("PointLight").Enabled = true
MissileClone:WaitForChild("Smoke").Enabled = true
MissileClone:WaitForChild("FlySound").Playing = true
MissileClone:WaitForChild("ExplosionScript").Enabled = true
end
end