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?
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)
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…
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)
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.