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.
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 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)
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.
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.
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
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.)
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!