How to make a Part face the mouse

I need help with a script to make a part face the mouse, I alredy have the position of the mouse in a 3d space every step, I Have looked for Way too much time and I am desperate, ive always found the same three anwsers, but also There is one of them that might work, it didnt work before because I couldnt get the mouse position every frame, but im not sure if it will work now either

local debounce = false
local cooldown = 2
script.Parent.Activated:Connect(function()
	if debounce == false then
		spawn(function()
			debounce = true
			local GB = script.Parent.GasterBlaster:Clone()
			GB.Parent = workspace
			spawn(function()
				GB:SetPrimaryPartCFrame(script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(3, 3, 0))
			end)
			local MousePos = script.Parent:WaitForChild("gg").Value
			GB.PrimaryPart.CFrame = CFrame.lookAt(GB.PrimaryPart.Position, MousePos)
			GB:WaitForChild("Destroy").Disabled = false
			GB:WaitForChild("Shoot").Disabled = false
			GB:WaitForChild("Creator").Value = script.Parent.Parent
				wait(cooldown)
			debounce = false
		end)
	end
end)
1 Like
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local part = --Your part here
	
while task.wait() do
	part.CFrame = CFrame.lookAt(part.Position,mouse.Hit)
end

This makes the part look at the mouse’s hit.

1 Like

Doesnt work, and btw it is the primary part of a mesh if you need that information

a meshpart or a mesh in a part?

it is a lot of meshparts and a normal part mixed to make a model, im trying to rotate the primary part

Well it look like you have it right, you’re setting the GB’s primary part.CFrame to CFrame.lookAt so it should work, can you maybe post a screenshot of it?

What do you need me to post a screenshot of?

The model. When you run the script does it error, or just look wrong?

It does not rotate at all, it does not give an error, that part of the script does not change anything at all


This is for one part.

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local part = workspace.Part --Your part here

	while task.wait() do
	part.CFrame = CFrame.lookAt(part.Position,mouse.Hit)
end


This is for a model.

local mouse = game:GetService("Players").LocalPlayer:GetMouse()
--local part = workspace.Part --Your part here
local model = workspace.Model

while task.wait() do
	model:PivotTo(CFrame.lookAt(model.PrimaryPart.Position,mouse.Hit.Position))
end

Doesnt work, i think its becuase local player doesnt work with a server script

Yeah, if you want it to be local-to-server, that might take some work.

i do have a localscript that sends the information to a vector3 value, here it is:

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local client = players.LocalPlayer
local mouse = client:GetMouse()

local tool = script.Parent
local value = tool:WaitForChild("gg")

local equipped = false

game:GetService("RunService").RenderStepped:Connect(function()
	if equipped then
		value.Value = mouse.Hit.Position
	end
end)

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)


local script in tool:

local players = game:GetService("Players")
local runService = game:GetService("RunService")

local client = players.LocalPlayer
local mouse = client:GetMouse()

local tool = script.Parent
local value = tool:WaitForChild("gg")
--local remoteF = tool.RemoteEvent
local remoteF = workspace.Model.RemoteEvent

local equipped = false

game:GetService("RunService").RenderStepped:Connect(function()
	if equipped then
		value.Value = mouse.Hit.Position
		remoteF:FireServer(value.Value)
	end
end)

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

server script in model:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player,value)
	script.Parent:PivotTo(CFrame.lookAt(script.Parent.PrimaryPart.Position,value))
end)

heres the place if you want it
modellook.rbxl (37.2 KB)

1 Like

i think theres a misunderstanding here, i need it to look to the mouse when clicked, and not continously, now of course, this is still useful for another idea i had, so thank you for that

oh, no, no, i didnt mean making it face the mouse when you have a tool, the tool makes the part appear and shoot, you need the mouse to aim, thats where we were wrong, but i think this will help me either way

well, after a while of testing, that didnt work, it instead just put GB under me rather than at my side

local debounce = false
local cooldown = 2
script.Parent.Activated:Connect(function(player,value)
	if debounce == false then
		spawn(function()
			debounce = true
			local GB = script.Parent.GasterBlaster:Clone()
			GB.Parent = workspace
			spawn(function()
				GB:SetPrimaryPartCFrame(script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(3, 3, 0))
			end)
			local MousePos = script.Parent:WaitForChild("gg").Value
			GB:PivotTo(CFrame.lookAt(Vector3.new(GB.PrimaryPart.Position),Vector3.new(value)))
			GB:WaitForChild("Destroy").Disabled = false
			GB:WaitForChild("Shoot").Disabled = false
			GB:WaitForChild("Creator").Value = script.Parent.Parent
				wait(cooldown)
			debounce = false
		end)
	end
end)

So you’re trying to make a bullet look at the pos of the mouse and then fly right? And is it a model or just one part?

Think of it this way, when you click, a turret appears that shoots one big blast to the mouse

Is this what you mean? I forgot I made some random gun thing and left it in the game.