How can I add 'spread' to a 'hit-scan' gun?

Problem:
I want to add a basic spread system to a simple raycast, similar to a shotgun
.
Drawing
image

.
Source Code:

	local raycastResult = workspace:Raycast(script.Parent.Muzzle.Position,(mousePos - script.Parent.Muzzle.Position).Unit*100,raycastParams)

Any help would be appreciated, Im not the best with raycasting, so im just stuck here.

1 Like

Since you are using raycasts, it is pretty easy to add spread. All you have to do, is add a randomness to your Raycast Direction.

(mousePos - script.Parent.Muzzle.Position).Unit*100 + (Vector3.new(
    math.random(-100,100),
    math.random(-100,100),
    math.random(-100,100)
)/90)) --Higher division, the lesser spread you will have. 100 is 1 in each direction.
5 Likes

Forgot to ask, where would I place raycastParams, because my gun keeps on damaging my character?

RaycastParams has a filter which allows you to put a table which things inside that
table will be ignored by the raycast, as well as a filter type, a bool to ignore water and so on. You could pop any part that the gun keeps damaging on your character inside your raycastparams like this:

local playerparts = {PlayerHead,PlayerTorso,PlayerRightArm}
local raycastparams = RaycastParams.new(playerparts,Enum.RaycastFilterType.Blacklist)

The playerparts table would be a table containing instances you’d want the raycast to deal with.
The RaycastFilterType then comes in asking if you want it to either:
Blacklist the parts inside the playerparts table, meaning it would ignore those parts inside of the table
or
Whitelist the parts inside the playerparts table, meaning it would ignore everything except those parts inside of the table.

Then when you’re done editing your RaycastParams, pop the variable at the end of your raycastresult variable’s parameters. If you need more info, here’s the wiki page on RaycastParams.

All of my other guns just have the same script without spread and it works, but, this script doesn’t.
I mean like where to place my raycastParams. I just use player.Character for Params and it always works, but this one doesn’t.
Script:

local raycastResult = workspace:Raycast(script.Parent.Muzzle.Position,(mousePos - script.Parent.Muzzle.Position).Unit*100 +
		(mousePos - script.Parent.Muzzle.Position).Unit*100 + (Vector3.new(
		math.random(-100,100),
		math.random(-100,100),
		math.random(-100,100),
		raycastParams --params
		)/90))
local raycastparams = RaycastParams.new()
raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
raycastparams.FilterDescendantInstances = {game.Players.LocalPlayer.Character}
--Change the LocalPlayer to something else if its on the server.
raycastparams.IgnoreWater = true

local raycastResult = workspace:Raycast(script.Parent.Muzzle.Position,(mousePos - script.Parent.Muzzle.Position).Unit*100 +
		(mousePos - script.Parent.Muzzle.Position).Unit*100 + (Vector3.new(
		math.random(-100,100),
		math.random(-100,100),
		math.random(-100,100),
		)/90),raycastParams)
1 Like


I get this error

Did you check for any errors yourself? I don’t have Studio open so I can’t check right now. I would assume that the mistake is related to line 14 in the ServerFireV2 script though.

image
im not sure on how to fix it usually this is and easy fix but where ever I remove it or move it somewhere it just doesnt work

maybe turn the multiple lines into one line and remove everything. Then start adding and fixing things if they go wrong.

Also I suggest having different colors for your scripting, as it is easier to see the red underline when it pops out and brackets sort of take that away.

1 Like

Would this work fine? It usually works but specifically on this gun with spread it still is not detecting and it still damages the humanoid that controls the gun
this is on the server
Also thanks for helping currently

raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

ADDITIONAL CODE:


script.Parent.CTRL.FS.OnServerEvent:Connect(function(player,mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Muzzle.Position,(mousePos - script.Parent.Muzzle.Position).Unit*100 +
		(mousePos - script.Parent.Muzzle.Position).Unit*100 + (Vector3.new(math.random(-25,25),math.random(-25,25),math.random(-25,25),raycastParams)))

Have you tried it? If it works then great, if not then try moving the raycastparams part to the right of the bracket.

Sorry but can you please elaborate or show me how?
Im confused on the wording of your sentece.

robloxapp-20220422-1645275.wmv (4.1 MB)
Sometimes the shotgun(The gun with spread) does damage, but the ones without spread never damaged me. I have tested the normal weapons without spread for hours and they never damaged me.
Normal:Weapon Script WITHOUT SPREAD

script.Parent.CTRL.FS.OnServerEvent:Connect(function(player,mousePos)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(script.Parent.Muzzle.Position,(mousePos - script.Parent.Muzzle.Position).Unit*100,raycastParams)

Maybe have an if statement checking if raycastResult.Instance.Parent.Name ~= Player.Name and only damage then. (It will check if the raycast is hitting the player.)

Would work but then how would you make the raycast ignore the owner of the gun, raycast params is not working

image

Im just going to make a seperate topic since I only asked for spread.
Even though we did not solve the second problem, I appreciate the help, as you did add spread!

1 Like

Simple thing to do would be to offset the origin or the raycast farther away so it wont touch the player. Sorry for the late response.