What you need to do, is make sure this bullet is not anchored (By default an instance that was created comes unanchored) and add a body velocity!
But to make something move towards the mouse, we must have the must have the mouse position, so in the local script that you are firing that event from you must include a parameter containing the position of the mouse.
After getting it, you have to create a BodyVelocity inside the bullet and make it move towards the mouse position!
To do it, you must play with the Velocity of the BodyVelocity.
But what is a bullet without spread? I've added a calculation to make the bullet spread as much as you'd like, you just need to change the value of "Spread".
If you want the bullet to go fast/slow, you just need to change the "300" value that I included in the script to something like 150, 100. Depends on the gun really!
To finish, you must add something to destroy the bullet after a bit if it doesn't hit anything. I've used the Debris system.
local repStorage = game:GetService("ReplicatedStorage")
local remote = repStorage:WaitForChild("Bulletfly")
local Bulletscript = repStorage:WaitForChild("BulletScript")
remote.OnServerEvent:connect(function(player, f, player, MousePosition)
local IAMAPEWPEW = Instance.new("Part", game.Workspace.Pellets)
IAMAPEWPEW.Name = "Pellet"
IAMAPEWPEW.Material = "Plastic"
IAMAPEWPEW.BrickColor = BrickColor.new("Institutional white")
IAMAPEWPEW.Size = Vector3.new(0.1,0.1,0.1)
IAMAPEWPEW.Shape = "Ball"
IAMAPEWPEW.Position = game.Workspace[player].Gun.Handle.Position
local BV = Instance.new("BodyVelocity",IAMAPEWPEW)
BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
local Spread = 0.005
local targetPoint = MousePosition
local shootDirection = (targetPoint - game.Workspace[player].Gun.Handle.Position).unit
shootDirection = CFrame.Angles((0.5 - math.random()) * 7 * Spread,
(0.5 - math.random()) * 7 * Spread,
(0.5 - math.random()) * 7 * Spread) * shootDirection
BV.Velocity = shootDirection * 300
game:GetService("Debris"):AddItem(a,3)
end)
Tell me if you find any problems!
2 comments
I think you need to show us what is in the "Bullet Script"
In the "Bullet Script" is just a script that detects if there is a humanoid, checks if it has the player name, and if not it kills the humanoid