How can i add a spread system for my hitscan weapons?

so, i have been trying to make some hitscan weapons for a game i am making, they work pretty well but i want to add a spread system to them, where some weapons like shotguns have more bullet spread and others weapons like rifles have less bullet spread, how can i do something like that?

my current code (it is on a local script):

local Tool = script.Parent
local Player = game.Players.LocalPlayer
Tool.Equipped:Connect(function(Mouse)
	local Tip = Player.Character:WaitForChild("HumanoidRootPart")
	Tool.RayS.Enabled = false
	Mouse.Button1Down:Connect(function()
		script.Disabled = true
		local ray = Ray.new(Tip.CFrame.p,(Mouse.Hit.p - Tip.CFrame.p).unit * 300)
		local Part, HitPos = game.Workspace:FindPartOnRay(ray,Player.Character,false,true)
		local End = Instance.new("Part",game.Workspace)
		local Att = Instance.new("Attachment")
		End.Transparency = 1
		End.CanQuery = false
		End.CanCollide = false
		End.CanTouch = false
		End.Size = Vector3.new(0.1,0.1,0.1)
		End.Anchored = true
		Att.Parent = End
		Tool.RayC.Attachment1 = Att
		local Distance = (Tip.CFrame.p - HitPos).Magnitude
		if Distance > 60 then
			Distance = 60
		end
		Tool.Beam:FireServer(Distance,HitPos)
		End.CFrame = CFrame.new(Tip.CFrame.p,HitPos) * CFrame.new(0, 0,-Distance)
		game:GetService("Debris"):AddItem(End,0.1)
		if Part then
			if Part.Parent:FindFirstChild("Humanoid") and Distance < 60 then
				Tool.Damage:FireServer(Part)
			end
		end
		wait(2)
		script.Disabled = false
	end)
end)

Firstly, in order to properly format code you need to add ``` before and after your code.
Example:

local exampleText = "This is an example!"
print(exampleText .. "_" .. 123)

One way add spread is to manipulate the target position. So instead of using Mouse.Hit.p you would save it and then add a randomized Vector3 on it. You might need to make sure it is on the same plane as Mouse.Hit.p relative to the Camera.

1 Like

so, something like this?


* local spreaded = CFrame.new(Mouse.Hit.p * CFrame.new(Vector3.new(math.random(-1,1),math.random(-1,1),math.random(-1,1))))

i just understanded what you tried to tell me, it helped a lot, thanks!

1 Like