Gun system: bullet generating

I’m currently working on a gun system but I ran into some problems.
when I generate the bullet, for some reason it spawns way ahead of the spawnpart that is located in the gun. I put the game link under the code for anyone who wants to check it out.

code of bullet spawn script:

local tool = script.Parent.Parent.Parent

local fireEvent = tool.System.events.FireEvent

local Debris = game:GetService(“Debris”)

fireEvent.OnServerEvent:Connect(function(player, MousePosition, BulletPosition)
– create bullet
local bullet = Instance.new(“Part”)
bullet.Parent = game.Workspace
bullet.CFrame = CFrame.new(BulletPosition, MousePosition)
bullet.Size = Vector3.new(0.1, 0.1, 1)
bullet.Material = Enum.Material.Neon
bullet.CanCollide = false
bullet.BrickColor = BrickColor.new(“Bright yellow”)

-- make bullet damage
local damageScript = tool.System.Storage.DamageScript:Clone()
damageScript.Parent = bullet

-- shoot bullet
local distance = (MousePosition - BulletPosition).Magnitude
local speed = 500
bullet.Velocity = bullet.CFrame.LookVector * speed
Debris:AddItem(bullet, 0.5)

end)

game:
https://www.roblox.com/games/7198314980/gun-system-try-6

is there anyone who knows how I can solve it. I’m trying to get the bullets to fly like in bad business, D-Day, phantom forces.

When you create the bullet make sure your setting its position to the bullet spawn part.

I published it to the game now with “bullet.Position = BulletPosition” in it but it still doesn’t change

Set its position to the name of the part you want it to shoot from.

it is, I send the location of that part through an event, that’s why its called different

I highly suggest you putting in the use of RayCasting, Its much more efficient and cleaner. However back to the answer to your topic:

Set the CFrame to the Nozzle of the gun (The front part you use to shoot) and then from there just Change the Velocity. Here is a example:

local Bullet = Instance.new("Part")
Bullet.Position = Gun.Hole -- Hole is the front part of the gun (The Nozzle)

I also suggest that you create a part slightly infront of the gun where the bullets will spawn, (So it doesn’t look like the bullets are coming out of the Body of the Gun.)

is it me not understanding it, but I think I already did all of that.
this is my code currently after some help of tnt_Dante:

local tool = script.Parent.Parent.Parent

local fireEvent = tool.System.events.FireEvent

local Debris = game:GetService(“Debris”)

fireEvent.OnServerEvent:Connect(function(player, MousePosition, BulletPosition)
– create bullet
local bullet = Instance.new(“Part”)
bullet.Parent = game.Workspace
bullet.CFrame = CFrame.new(BulletPosition, MousePosition)
bullet.Size = Vector3.new(0.1, 0.1, 1)
bullet.Material = Enum.Material.Neon
bullet.CanCollide = false
bullet.Position = BulletPosition
bullet.BrickColor = BrickColor.new(“Bright yellow”)

-- make bullet damage
local damageScript = tool.System.Storage.DamageScript:Clone()
damageScript.Parent = bullet

-- shoot bullet
local distance = (MousePosition - BulletPosition).Magnitude
local speed = 500
bullet.Velocity = bullet.CFrame.LookVector * speed
Debris:AddItem(bullet, 0.5)

end)

Hey, did you ever fix this issue? Im having the same problem