How do I add a spread to a raycasting gun?

So, hello, I’m currently making a shotgun(Used my old model which was a laser gun), I don’t know how to add a spread in it, and I’m of course, bad at maths, but I put a for loop in it, I know this type of question was already asked, but I don’t understand those. Here’s my code:

local BeamEvent = script.Parent:WaitForChild("Beam")

local debris = game:GetService("Debris")

BeamEvent.OnServerEvent:Connect(function(Player, laserpos, target)
	local ray = Ray.new(laserpos, (target - laserpos).Unit*550)
	local hit, position = game.Workspace:FindPartOnRay(ray, nil)
	pcall(function()
		for i = 1, 4 do
			local HumanoidToDamage = hit.Parent:FindFirstChild("Humanoid")
			if HumanoidToDamage and HumanoidToDamage ~= Player.Character.Humanoid then
				HumanoidToDamage:TakeDamage(99)
				print(HumanoidToDamage.Parent.Name .. " just got hit by beam. ")
			end
			local Beam = Instance.new("Part", game.Workspace)
			Beam.Name = "Beam"
			Beam:SetNetworkOwner(nil)
			local dist = (target - laserpos).magnitude
			Beam.Size = Vector3.new(0.3,0.3, dist)
			Beam.CFrame = CFrame.new(laserpos,position) * CFrame.new(0,0,-dist/2)
			Beam.Anchored = true
			Beam.CanCollide = false
			Beam.BrickColor = BrickColor.new("Really red")
			Beam.Material = Enum.Material.Neon
			debris:AddItem(Beam, .1)
			print("Beam Destroyed")
		end
	end)
	
end)

Help is very, very, very appreciated

1 Like

You could probably do

laserpos = laserpos + Vector3.new(math.random(0,1),math.random(0,1),math.random(0,1))

change math random to whatever
If you want a smaller value you can
math.random(0,100)/100

The first time I made a raycast shotgun, I followed this guy’s steps. It may help you out.

1 Like

I’ma try this one, if doesn’t work then I’ll watch that video the guy provided. Anyways, thanks.

1 Like