Help with shotgun spread in raycasting

So I am trying to make a shotgun spread with my raycasting gun is there any way I could work this. I have tried already but the gun shoots in a weird way. When I make it shoot 1 bullet it works fine. Any help is appreciated!

Heres the main part of the script:

for ShotNumber = 1, GunInMod[“BulletPerShot”] do
local NewOrigin
if GunInMod[“BulletPerShot”] > 1 then
NewOrigin = Origin + Vector3.new((math.random(-100, 100)/100), (math.random(-100, 100)/100), (math.random(-100, 100)/100))
else
NewOrigin = Origin
end
local Direction = (MousePos - NewOrigin).Unit * 300
local Result = WS:Raycast(NewOrigin, Direction, rParams)
local Intersection = Result and Result.Position or NewOrigin + Direction
local Distance = (NewOrigin - Intersection).Magnitude

	local Bullet = ReplicatedStorage.Bullets[GunInMod["BulletType"]]:Clone()
	Bullet.Size = Vector3.new(0.1, 0.1, Distance)
	Bullet.CFrame = CFrame.new(Origin, (Intersection + NewOrigin)) * CFrame.new(0, 0, -Distance/2)
	Bullet.Parent = WS["Bullets/Tracers"]
	
	if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Result.Instance.Parent:FindFirstChild("Humanoid")
		if Result.Instance.Name == "Head" then
			ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, true)
			Humanoid:TakeDamage(GunInMod["Damage"] * GameSettings["GunSettings"]["HeadshotBonus"])
		else
			ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, false)
			Humanoid:TakeDamage(GunInMod["Damage"])
		end
	end
	
	spawn(function()
		task.wait(0.1)
		--Bullet:Destroy()
	end)
end

2 Likes

I don’t know about your bug but please try this
I made can collide false and massless true, also don’t use destroy I made for you debris so I hope it’s gonna work


local Bullet = ReplicatedStorage.Bullets[GunInMod["BulletType"]]:Clone()
	Bullet.Size = Vector3.new(0.1, 0.1, Distance)
	Bullet.CFrame = CFrame.new(Origin, (Intersection + NewOrigin)) * CFrame.new(0, 0, -Distance/2)
	Bullet.Parent = WS["Bullets/Tracers"]
	Bullet.CanCollide = false
        Bullet.Massless = true

	if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Result.Instance.Parent:FindFirstChild("Humanoid")
		if Result.Instance.Name == "Head" then
			ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, true)
			Humanoid:TakeDamage(GunInMod["Damage"] * GameSettings["GunSettings"]["HeadshotBonus"])
		else
			ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, false)
			Humanoid:TakeDamage(GunInMod["Damage"])
		end
	end
	
	spawn(function()
		game.Debris:AddItem(Bullet,0.1)
	end)
end

:AddItem() does not yield, you don’t need to use spawn(). You should also use task.spawn() by the way!

1 Like

The bullets are already massless and cancollide off. Thanks for letting me know about the debris. Also I have made changes to the script, the bullet works fine but I want to add something which im not sure how to add. The bullets dont actually come out of the gun, so how would I make this?

Heres the new script:
for ShotNumber = 1, GunInMod[“BulletPerShot”] do
local NewOrigin
if GunInMod[“BulletPerShot”] > 1 then
NewOrigin = Origin + Vector3.new((math.random(-100, 100)/100), (math.random(-100, 100)/100), (math.random(-100, 100)/100))
else
NewOrigin = Origin
end
local Direction = (MousePos - NewOrigin).Unit * 300
local Result = WS:Raycast(NewOrigin, Direction, rParams)
local Intersection = Result and Result.Position or NewOrigin + Direction
local Distance = (NewOrigin - Intersection).Magnitude

	local Bullet = ReplicatedStorage.Bullets[GunInMod["BulletType"]]:Clone()
	Bullet.Size = Vector3.new(0.1, 0.1, Distance)
	Bullet.CFrame = CFrame.new(NewOrigin, Intersection) * CFrame.new(0, 0, -Distance/2)
	Bullet.Parent = WS["Bullets/Tracers"]
	
	if Result and Result.Instance.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Result.Instance.Parent:FindFirstChild("Humanoid")
		if Result.Instance.Name == "Head" then
			ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, true)
			Humanoid:TakeDamage(GunInMod["Damage"] * GameSettings["GunSettings"]["HeadshotBonus"])
		else
			ReplicatedStorage.Events.UI.Hitmarker:FireClient(Player, false)
			Humanoid:TakeDamage(GunInMod["Damage"])
		end
	end

	Debris:AddItem(Bullet, 0.1)
end

PS: Theres a invisible part called “Muzzle” where the bullets come from

Well debris it’s better than destroy

What do you mean?
you mean it’s shoots from another position not from the weapon?

Well in the picture the bullets are not coming from a single position but rather different positions.

1 Like

Add attachment or a part and weld it in the position and make it starts raycasting from it try that, idk if attachments gonna work do welded part

Bullet.CFrame = Weldedpart.CFrame * CFrame.new(NewOrigin, Intersection) * CFrame.new(0, 0, -Distance/2)

Idk if it’s gonna work 100% but try it and try to fix the cframe me if it’s not working (I’m bad at cframe so I couldn’t)

Unfortunately this didnt work.