I’m going to keep it short and simple with this one. How do I make a part keep facing towards where the player’s mouse is hovering over, only while they are in the vehicleseat?
I’ve tried to dissect code from turrets in the toolbox, and tried to ask the Roblox Assistant, but nothing worked. Help please
The simplest way to get the player’s mouse location is to use the mouse object’s .Hit property. Then, you can use CFrame.LookAt to point the part at the position. To only run while the player is in the seat, you’d check if the seat’s occupant is the player whenever the occupant property changes.
Example:
local players = game:GetService("Players")
local runService = game:GetService("RunService")
local localPlayer = game.Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local part = workspace.Part -- the target part
local seat = workspace.Seat -- the target seat
function update()
local mousePos = mouse.Hit.Position
part.CFrame = CFrame.lookAt(part.Position,mousePos)
end
local connection
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local occupant = seat.Occupant
if connection then connection:Disconnect() end
if localPlayer.Character == nil then return end
if seat.Occupant == nil or seat.Occupant.Parent ~= localPlayer.Character then return end -- make sure the player is in the seat
connection = runService.RenderStepped:Connect(update)
end)
I placed a part on the workspace (added a decal so I knew where the front face was), also anchored the part …
Then I made a local script in StarterGui, could also be in StarterCharacterScripts or StarterPlayerScripts (just so the script can reference the player locally) …
local rns=game:GetService("RunService")
local player=game:GetService("Players").LocalPlayer
local root=workspace:WaitForChild("Part")
local mouse=player:GetMouse()
while true do rns.Stepped:Wait()
local rootPos = root.Position
local mousePos = mouse.Hit.Position
root.CFrame = CFrame.new(rootPos, Vector3.new(mousePos.X, rootPos.Y, mousePos.Z))
end