Making a part face where the player's mouse is hovering while in a VehicleSeat

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)

Hope this helps!

1 Like

When I sit down, it doesn’t appear to do anything, no output either. this is for localscript right?

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

GetMouse() is to be used locally.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.