Pointing gun to mouse aim direction

Hello, I’m trying to make a gun that points to where your mouse is but my script is not working. Can someone help me?

local seat = script.Parent.Seat
local mesh = script.Parent.MeshPart

seat.Touched:Connect(function(touch)
	local h = touch.Parent:FindFirstChild("Humanoid")
	if h then
		wait(1)
		if seat.Occupant ~= nil then
			local seated = true
			print("Player is seated")
			local player = game.Players:GetPlayerFromCharacter(h.Parent)
			print(player.Name)
			local mouse = player:GetMouse()
			print("Got the mouse")
			while wait(0.1) do
				mesh.CFrame = CFrame.new(mesh.Position, mouse.CFrame)
			end
		end
	end
end)
1 Like

If this is a server-sided script, it wouldn’t work. You can only get the mouse in a localscript as the mouse isn’t replicated to the server. I would fire a remove event to check the mouse’s position.

1 Like

But not even the stuff i put there prints

Also, if I need a local script to get the mouse and the localscript is under a model in workspace, It wouldnt run, so where do I put it

Put it in StarterPlayerScripts

2 Likes

I’ve changed your post title to be more descriptive of the issue you need support with.

2 Likes

Is there any way to determine the position of the mouse with vector3? since it doesnt let me use cframe.

There is the Hit property of Mouse which will give you the position of where the mouse is hovering over in the world. Is that what you’re looking for?

1 Like

Try Mouse.Position, I think that would work.

1 Like

Mouse.Position doesn’t exist as a property. I’m assuming you’re referring to its X and Y properties but those are in screen space which is not want OP wants.

1 Like

I meant to say Mouse.Hit.Position, my bad

1 Like

the best option would be to use 2 scripts in my game I had it like this

create a script and that the seat is the parent from the same script you create a local script and when the player sits down copies the local script to his character to later activate it the control part must be in the local script and also from the script normal when the player jumps you have to delete the local script

I hope it works for you Luck

Translated with google translator

1 Like

Hablas español no? 30…

Si hablo español puedo traducir un poco el ingles pero aun no se escribirlo