How do you make a gun bullet move to the player's mouse position?

So I wanted to script a gun to make it fire a bullet at the player’s mouse position but I don’t know how to get the bullet to fire at the player’s mouse position.

Currently the bullet will move towards the gun’s LookVector so I need someone to tell me how to make it move to the player’s mouse position instead.

Here’s how the code works:

Server code
local bulletClone = bullet:Clone() -- 'bullet' is a variable that holds a reference to the gun's bullet
bulletClone.Anchored = false
bulletClone.CanCollide = false
bulletClone.CFrame = plrPistolObj.CFrame -- This is where the bullet will be at when it gets to the 'bullets' folder
bulletClone.Parent = bullets -- 'bullets' is a folder that contains all bullets to prevent the workspace from being spammed with bullets
		
local bodyForce = Instance.new("BodyForce") -- I'm using BodyForce to move the bullet
bodyForce.Force = plrPistolObj.CFrame.LookVector * bulletSpeed -- This is where the bullet is moving to
bodyForce.Parent = bulletClone

The client code is all about just firing a remote event whenever the gun tool is clicked with the Activated event.

So how do I make the gun bullet move to the player’s mouse position?

4 Likes

Ill give you a pretty simple solution

The Setup:
image
ClientSide:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	script.Parent.Fire:FireServer(Mouse.Hit.p)
end)

ServerSide:

local bulletClone = bullet:Clone() -- 'bullet' is a variable that holds a reference to the gun's bullet
bulletClone.Anchored = false
bulletClone.CanCollide = false
bulletClone.CFrame = plrPistolObj.CFrame -- This is where the bullet will be at when it gets to the 'bullets' folder
bulletClone.Parent = bullets -- 'bullets' is a folder that contains all bullets to prevent the workspace from being spammed with bullets


script.Parent.Fire.OnServerEvent:Connect(function(Player,MousePos)
	local vel = Instance.new("BodyForce",bulletClone) -- I'm using BodyForce to move the bullet
	vel.Velocity = plrPistolObj.MousePos.LookVector * bulletSpeed -- This is where the bullet is moving to
end)

Hope This Works!

3 Likes

This wouldn’t work because I’m using BodyForce and not BodyVelocity so that would be vel.Force instead of vel.Velocity.

There are 2 problems with that:

This would error because “plrPistolObj” doesn’t have a thing called “MousePos” and even if you remove the “plrPistolObj” it would still error because you are trying to get the LookVector of a Vector3 and that won’t work too, you get LookVector of a CFrame but not a Vector3

1 Like

Instead of bulletSpeed change it to 100. This will go 100 studs further. I recommend using bodyposition instead of bodyforce.

1 Like

Could you explain why you are recommending me to use BodyPosition instead of BodyForce?

BodyForce works just fine, it’s just that I want to know how to make the bullet move towards the player’s mouse position.

1 Like

Ohhhh I used your code kinda and forgot to change body force to bodyvelocity

1 Like

if you change that it should work also make sure the bullet speed variable is 100

1 Like

Mousepos is from when your firing the server you didnt even read the client code…

1 Like

I still haven’t found a solution to this problem so if anyone has a solution then let me know.

1 Like

Why do you want to use a BodyForce instead of a BodyVelocity?

Ehh you might not need it anymore but here it is:

server

local event = script.Parent.Fire
local Debris = game:GetService("Debris")

local SPEED = 15

event.OnServerEvent:Connect(function(player, mouse, handle)
	local part = handle.BulletStart
	local Bullet = game.ReplicatedStorage.Bullet:Clone()
	Bullet.Position = part.Position
	Bullet.Parent = game.Workspace
	Bullet.BodyVelocity.Velocity = mouse.LookVector*SPEED
	Bullet.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			hit.Parent.Humanoid:TakeDamage(10)
		end
	end)
	Debris:AddItem(Bullet, 3)
end)

Client:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local event = script.Parent.Fire

script.Parent.Activated:Connect(function()
	event:FireServer(mouse.Hit, script.Parent.Handle)
end)
3 Likes

I suggest having the bullet move to were the Barrel of the gun is aiming, and having the gun point towards were you aim. With this you can implement real and accurate bullet drop, gun sway and Recoil. If you refer to the most successful shooter games, none of them are using Bullet to Cursor directly.

2 Likes