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.