Turret Rotation to Look at Mouse.Hit?

I’m new to coding and something I can’t really get my head around is the client/server boundary.

right now, I’m trying to use a bodygyro to move a turret to point directly at where the mouse is pointing.
I can’t get it to rotate. my code is below. please tell me what is wrong or point me in the right direction. Thanks :slight_smile:

local turret = script.Parent.turretgun
local bodyGyro = turret:WaitForChild(“BodyGyro”)
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
while true do
wait(.1)
bodyGyro.CFrame = CFrame.new(turret.Position, mouse.Hit.p)
end

Where is this script placed? Have you placed it inside the workspace or the starterplayerscripts?

it is inside of the turret itself. the workspace.

ive also tried

TurretSeat.Changed:Connect(function()
local player = game.Players:GetPlayerFromCharacter(TurretSeat.Occupant.Parent)
if player then
local mouse = player:GetMouse()
bodyGyro.CFrame = CFrame.new(turret.Position, mouse.Hit.p)
end
end)

Then there’s your problem. LocalScripts inside workspace won’t actually run, they will only load in. For a script to be run as you want it to you have to place it inside starterplayerscripts and then call a function that is placed in the workspace everytime you want to move the turret.

1 Like

how would I be able to do it if i switch it to a script rather than a local script?

You could use a module script, they’re probably the easiest and cleanest way to do it. If you need any help on how to set a module script up, look here: ModuleScript | Documentation - Roblox Creator Hub

so have the script require a module script that wont run until a player enters the seat?

1 Like

you could also do the movement completely locally so you grab the turret in the startplayerscripts script and there change the direction and rotation of the turret.