Help on making a gun system

I want to make a gun system for snipers, pistols, shotguns and SMG’s.

I have tried raycasting and it has worked, but I don’t know how to make the bullet move and where do i set the bullet position.

I have looked on the dev hub and dev forum with no luck.

Here is my code:

Server:

-- made by recanman
game.ReplicatedStorage.Assets.Events.CreateBullet.OnServerEvent:Connect(function(plr, bulletType)
	local clone = game.ReplicatedStorage.Assets.Bullets[bulletType]:Clone()
	clone.Parent = game.Workspace.BulletStorage
end)

Client:

-- made by recanman
local tool = script.Parent
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local maxAmmo = script.Settings.MaxAmmo.Value
local reloadSpeed = script.Settings.ReloadSpeed.Value
local shellRange = script.Settings.ShellRange.Value
local shellDamage = script.Settings.ShellDamage.Value
local gunType = script.Settings.GunType.Value

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {tool.Handle, plr.Character}
params.IgnoreWater = true

tool.Activated:Connect(function()
	local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * shellRange)
	local result = game.Workspace:Raycast(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * shellRange, params)
	
	local beam = game.ReplicatedStorage.Assets.Bullets[gunType .. "Bullet"]:Clone()
	beam.Parent = game.Workspace.BulletStorage
	beam.Orientation = plr.Character.HumanoidRootPart.Orientation
	local distance = tool.Handle.CFrame.p.magnitude

	game.ReplicatedStorage.Assets.Events.CreateBullet:FireServer(beam.Name)
	beam:Destroy()
	
	if (result ~= nil) then
		print(result.Instance:GetFullName())
	end
end)

I would also like to know how to make multiple bullets and spread them out like a shotgun.

2 Likes

The best possible way I currently think to pull this off is using body movers, preferably BodyVelocity. It’ll be easy to make different projectile speeds and simulate bullet drop

I suggest reading this: Writing an FPS framework (2020)
and this: Writing an FPS framework, part 2 (2020)

2 Likes

I tried that, but how do I set the position of the bullet? I set the position fine, but it launches in the wrong direction and the Orientation is all wrong

I’m making a third-person shooter

1 Like

I tried that, but how do I set the position of the bullet? I set the position fine, but it launches in the wrong direction and the Orientation is all wrong

EDIT: oops i posted this twice

1 Like
Bullet.Position = YourBarrel.Position
Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.Position) --make it face the ,ouse positiob
BodyVelocity.Velocity = Bullet.CFrame.lookVector * 50 --50 is the speed, make sure to create bodyvelocity first
2 Likes