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
	local Tip = Player.Character:WaitForChild("HumanoidRootPart")
	Tool.RayS.Enabled = false
		script.Disabled = true
		local ray =,(Mouse.Hit.p - Tip.CFrame.p).unit * 300)
		local Part, HitPos = game.Workspace:FindPartOnRay(ray,Player.Character,false,true)
		local End ="Part",game.Workspace)
		local Att ="Attachment")
		End.Transparency = 1
		End.CanQuery = false
		End.CanCollide = false
		End.CanTouch = false
		End.Size =,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.CFrame =,HitPos) *, 0,-Distance)
		if Part then
			if Part.Parent:FindFirstChild("Humanoid") and Distance < 60 then
		script.Disabled = false

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

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 = *,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