Shotgun Spread Not Working

Can someone please help? I don’t know what I’m doing wrong, and the problem is still present

1 Like

iterate over the shoot() function for every shot using a for loop

I know how to do the shooting, I just need help with connecting the spread function with the shooting, so the shotgun has spread

Try using this link. I think it’s a good way to make a shotgun.

Shotguns || The Evil Duck

Ok so I followed along with the tutorial, but there still isn’t a spread in the shotgun, only one ray pops up still…

local maxGunRange = 100
local camera = workspace.CurrentCamera
local debris = game:GetService("Debris")
game.ReplicatedStorage.ShotgunFire.OnServerEvent:Connect(function(player, Character, HEADSHOT, BODY_DAMAGE, SHOOTY_PART, MousePos, HandleOfGun, Part_1, Part_2, Part_3, mouse)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastParams.FilterDescendantsInstances = {Character, SHOOTY_PART, HandleOfGun, Part_1, Part_2, Part_3}
	local Dir = CFrame.new(SHOOTY_PART.Position, MousePos)*CFrame.Angles(math.rad(math.random(-20,20)), math.rad(math.random(-20,20)),0)
	local ray = workspace:Raycast(SHOOTY_PART.Position,Dir.LookVector*maxGunRange)
			local part = Instance.new("Part")
	        local Dist = (ray.Instance.Position-SHOOTY_PART.Position).Magnitude
	        part.CFrame = Dir*CFrame.new(0,0,-Dist)
	        part.Size = Vector3.new(0.1,0.1,Dist*2)
			part.Anchored = true
			part.CanCollide = false
			part.Parent = workspace
			part.BrickColor = BrickColor.new("New Yeller")
    		debris:AddItem(part,0.1)
if ray then
		local part = ray.Instance
		print(part)
		local humanoid = part.Parent:FindFirstChild("Humanoid")
		print("HumanoidFound")
		
		if part.Name == "Head" then
			humanoid.Health = humanoid.Health - math.random(20,40)
		else
			humanoid.Health = humanoid.Health - math.random(10,15)
		end
	end
end)

Help please…

1 Like

Ok, is there something really obvious I’m doing wrong? If so, I still can’t see it

Video of shotgun: robloxapp-20200824-1355132.wmv

sorry for the delay. Here’s what I did.

local dir = CFrame.new(mainpos, mousehit) * CFrame.Angles((math.random(-spread,spread)/5,(math.random(-spread,spread)/4,0)
local ray = Ray.new(mainpos, dir.LookVector*20)

then cast the ray

game.ReplicatedStorage.ShotgunFire.OnServerEvent:Connect(function(Player,StartPos, TargetPos, Damage, MaxDistance, Spreading, BulletsPerShot)
	for i = 1,BulletsPerShot do
	local Dir = CFrame.new(StartPos, TargetPos)*CFrame.Angles(math.rad(math.random(-Spreading, Spreading)),math.rad(math.random(-Spreading, Spreading)),0)
	local RayCast = Ray.new(StartPos, Dir.LookVector*MaxDistance)
	local Target,Pos = game.Workspace:findPartOnRay(RayCast,Player.Character,false,true)
	if Target then
	local Hum = Target.Parent:FindFirstChild("Humanoid")
	if Hum then
		Hum:TakeDamage(Damage)
		end
		end
	
	local Vis = Instance.new("Part",game.Workspace)
	Vis.Anchored = true
	Vis.CanCollide = false
	local Dis = (Pos-StartPos).Magnitude
    Vis.CFrame = Dir*CFrame.new(0,0, -Dis)
    Vis.BrickColor = BrickColor.new("New Yeller")
    Vis.Size = Vector3.new(0.1,0.1,Dis*2)
    game.Debris:AddItem(Vis,0.1)
	end
end)

Spread works, but sometimes damages