Hi dev fourm… again
anyways I wanted to make a tank turret, simple right? I guess not because it wont work!
so i have a servo hinge connecting the turret to an anchored part so it can rotate around and another servo hinge connecting the turret to the gun so it can rotate up
however, using my script (made it quick so no gun rotation yet) it comes up with this error saying Workspace.KV-2 Turret.VehicleSeat.Script:15: attempt to index nil with ‘X’](rbxopenscript://www.dummy.com/dummy?scriptGuid=%7BB13C96EA%2DDF2C%2D4859%2DA123%2DA634B8AED1D2%7D&gst=3#15)
(here’s an image of my turret)
Heres my script, it has a parent for the vehicle seat sitting next to the turret
local Seat = script.Parent
Seat.Changed:Connect(function()
if Seat.Occupant ~= nil then
-- define some variables
local Occupant = script.Parent.Occupant.Parent
local Hinge = script.Parent.Parent.Part.TurretRotator
local RootPart = Hinge.Parent
local Player = game.Players:GetPlayerFromCharacter(Occupant)
local mouse = Player:GetMouse()
while wait() do
print(Occupant)
-- define position of origin
local HKpos = Vector2.new(RootPart.Position.X,RootPart.Position.Z)
-- define position of target
local position = Vector2.new(mouse.X, mouse.Y)
-- the ratio to get the triangle's width over the height
local ratio = (position.Y-HKpos.Y)/(position.X-HKpos.X)
local beta = math.atan(ratio) -- converting this to radians
-- turn it from radians into degrees
beta = beta*(180/math.pi)
print(beta)
Hinge.TargetAngle = -beta -- beta has to be negative so it points at you
end
end
end)
I basically got this script from a turret that would face the player but I modified it to point to the occupant of the seat’s mouse instead