Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make a shotgun fire like a shotgun?

Currently, I'm working on a shotgun, and right now it only fires one bullet at a time, as if it were a semi-auto rifle or pistol. However, I want it to fire like a shotgun. How can I do so? Here is the script for how I make the shotgun fire bullets (I am using RayCasting by the way):

Script:

if ammo.Value > 0 and not reloading then
		sparkGui.Enabled = true
		
		for i = 1, 8 do
			local randomValue1 = math.random(-spread.Value, spread.Value)
			local randomValue2 = math.random(-spread.Value, spread.Value)
			local ray = Ray.new(flare.CFrame.p, (mouse.hit.p - flare.CFrame.p).unit * range.Value)
			local touch, position = workspace:FindPartOnRay(ray, player.Character)
							
			if touch then
				OnHit:FireServer(touch)
			end
			
			if allowTracing.Value then
				local bullet = Instance.new("Part")
				bullet.Anchored = bullet
				bullet.CanCollide = false
				bullet.Transparency = 0.8
				bullet.BrickColor = BrickColor.White()
				bullet.Material = Enum.Material.SmoothPlastic
						
				local distance = (flare.CFrame.p - position).magnitude
				bullet.Size = Vector3.new(0, 0, distance)
				bullet.CFrame = CFrame.new(flare.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
				bullet.Parent = workspace
					
				game:GetService("Debris"):AddItem(bullet, 0.1)
			end
		end

4 comments

0

You need to create the bullets, freeze them, then make them fire.

0

after all 8 are created.

0

I did that, but it still fires like one bullet

0

I want it to spread, like a shotgun would

1 answer

You can use an array to store all of your bullets and their data inside.

if ammo.Value > 0 and not reloading then
        sparkGui.Enabled = true
	local bullets = {}
	local Index = 0
        for i = 1, 8 do
            local randomValue1 = math.random(-spread.Value, spread.Value)
            local randomValue2 = math.random(-spread.Value, spread.Value)
            local ray = Ray.new(flare.CFrame.p, (mouse.hit.p - flare.CFrame.p).unit * range.Value)
            local touch, position = workspace:FindPartOnRay(ray, player.Character)

            if touch then
                OnHit:FireServer(touch)
            end

            if allowTracing.Value then
                local bullet = Instance.new("Part")
                bullet.Anchored = true
                bullet.CanCollide = false
                bullet.Transparency = 0.8
                bullet.BrickColor = BrickColor.White()
                bullet.Material = Enum.Material.SmoothPlastic
                local distance = (flare.CFrame.p - position).magnitude
                bullet.Size = Vector3.new(0, 0, distance)
                bullet.CFrame = CFrame.new(flare.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
		index = index + 1
		bullets[index] = bullet
	   end
        end
	for i,bullet in pairs(bullets) do
		--You don't need to set it to workspace because debris already did it
		game:GetService("Debris"):AddItem(bullet, 0.1)
	end
end

Debris details: Wiki

This discussion is preserved as it was. New answers, comments, and votes are closed.