Bullets starting from a specific position instead of the muzzle

I’ve been making a gun system and I came across an issue I’ve never faced before. I don’t know how to fix it and I’ve tried to solve it. Also, I do not want the bullets to go to the mouse position, but forward starting from the muzzle position.

Here is the function for shooting (Server):

Remotes.FireGun.OnServerInvoke = function(player, Obj, Handle, Muzzle)
	local Bullet = Instance.new("Part")
	Bullet.Size =  Vector3.new(.1,.1,1)
	Bullet.CFrame = CFrame.new(Muzzle.Position)
	Bullet.Color = Color3.new(1, 0.772549, 0.0901961)
	Bullet.Material = Enum.Material.Neon
	Bullet.CanCollide = false
	Bullet.Anchored = false
	Bullet.Parent = workspace
	
	Bullet:SetNetworkOwner(player)
	
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Velocity = (Bullet.CFrame.LookVector * 100)
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Parent = Bullet
end

And here is the input detection function with the remote function:

Services.UserInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and Status.isEquipped then
		local FiredGun = Remotes.FireGun:InvokeServer(tool, tool:WaitForChild("Handle"), tool:WaitForChild("Handle"):WaitForChild("Muzzle"))
	end
end)

Video that shows the problem:

Any help is greatly appreciated!

Make sure the muzzle isn’t anchored and it is correctly welded to the gun.
To me it seems like that’s the issue.

It is an attachment in the handle.

Oh that explains it then, .Position inside of an attachment is an offset to the part containing the attachment, use .WorldPosition instead.

1 Like

It worked. But now it goes only to one direction. How will I make it so it goes to where the muzzle faces at?

Change this to
BodyVelocity.Velocity = (Muzzle.WorldCFrame.LookVector * 100)
Sorry for late answer.

1 Like

Okay now just rotate the attachment in the right way and you’re done.

1 Like

I tried many rotations, but I still can’t get it work properly.

Ty for the help! I fixed it with Roblox’s AI Assistant (somehow).

Remotes.FireGun.OnServerInvoke = function(player, Obj, Handle, Muzzle)
	local Bullet = Instance.new("Part")
	Bullet.Size =  Vector3.new(.1,.1,1)
	-- Make the bullet rotation the same as the muzzle
	Bullet.CFrame = CFrame.new(Muzzle.WorldPosition, Muzzle.WorldPosition + (Muzzle.WorldCFrame.LookVector))
	Bullet.Color = Color3.new(1, 0.772549, 0.0901961)
	Bullet.Transparency = .6
	Bullet.Material = Enum.Material.Neon
	Bullet.CanCollide = false
	Bullet.Anchored = false
	Bullet.Parent = workspace
	
	Bullet:SetNetworkOwner(player)
	
	local BodyVelocity = Instance.new("BodyVelocity")
	BodyVelocity.Velocity = (Muzzle.WorldCFrame.LookVector * 400)
	BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	BodyVelocity.Parent = Bullet
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.