How to make shotgun accuracy decrease in distance?

I am making a gun using raycasting, and i want to be able to make shotguns work like they do in other games. I already tried to write code to do this, but it spreads the same reguardless of distance. What should I do?
My spread code that i use for the shotgun:

local function doSpread(bool)
	local spread = gunSettings.Spread
	local random = Random.new()
	if bool or gunSettings.SpreadEnabled then
		return mouse.Hit*CFrame.new(random:NextNumber(-spread,spread),random:NextNumber(-spread,spread),0)
	else
		return mouse.Hit
	end
end
2 Likes

Multiply the spread by the shotgun’s distance, divided by a number (to prevent spread from getting out of control).