How do I add raycast spread for my particular setup?

I’ve been looking around for a while for solutions to this, and what seems to work for other people just breaks my script. I wanted to figure out how tomake sprad so I tried math.random, different choices for the script to pick, and so on. Nothing seems to work, and some just break the game.

local Debris = game:GetService("Debris")

script.Parent.rayCast.OnServerEvent:Connect(function(Player, StartPos, DestinationPos, MeshStart)
	local rayCast = Ray.new(StartPos, (DestinationPos - StartPos).Unit * 100000000000000)
	local Part,Position,Normal = game.Workspace:FindPartOnRay(rayCast,Player.Character, false,true)
	local Humanoid = Part.Parent:FindFirstChild("Humanoid") or game.Workspace.Humanoid
	local Part, Position = game.Workspace:FindPartOnRay(rayCast, Player.Character, false, true)
    if Humanoid then
		if Part.Name == "Head" then
			script.Parent.Hit:Play()
			Humanoid:TakeDamage(50)
    	else
     		Humanoid:TakeDamage(25)
    			end
	    	end		
	if not Humanoid then
		print("no")
	end

This is the part of my script that casts the ray and deals damage, lines 1 to 18.

1 Like
local Debris = game:GetService("Debris")

script.Parent.rayCast.OnServerEvent:Connect(function(Player, StartPos, DestinationPos, MeshStart)
        local mins = -7
        local maxs = 7
        local decimalPlace = "0."
        local newDest = DestinationPos + Vector3.new(decimalPlace ..math.random(mins,maxs),0,decimalPlace..math.random(mins,maxs))
	local rayCast = Ray.new(StartPos, (newDest - StartPos).Unit * 100000000000000)
	local Part,Position,Normal = game.Workspace:FindPartOnRay(rayCast,Player.Character, false,true)
	local Humanoid = Part.Parent:FindFirstChild("Humanoid") or game.Workspace.Humanoid
	local Part, Position = game.Workspace:FindPartOnRay(rayCast, Player.Character, false, true)
    if Humanoid then
		if Part.Name == "Head" then
			script.Parent.Hit:Play()
			Humanoid:TakeDamage(50)
    	else
     		Humanoid:TakeDamage(25)
    			end
	    	end		
	if not Humanoid then
		print("no")
	end
end)

hope this helps

this sadly also broke the script

2 Likes

Maybe something like this? Note this is not tested.

local Min,Max = -.7, .7;
local SpreadX,SpreadY = math.random(Min,Max), math.random(Min,Max)
local NewDestination = DestinationPos + Vector3.new(SpreadX, SpreadY, 0)
local rayCast = Ray.new(StartPos, (NewDestination - StartPos).Unit * 100000000000000)```
2 Likes

The reason I done it the way I done is because math.random doesn’t accept decimals.

Can you send a screenshot of the error if there is one it would help a lot

Thanks, that worked! I just changed the values from decimals though.

Oh that’s true, my bad.

local Min,Max = -.7, .7;
local Multiple = 10; -- All the accuracy we need.
local function GenerateSpread()
    -- Not super necessary to put it in a function but it's whatever.
    return math.random(Min*Multiple, Max*Multiple)/Multiple
end
local SpreadX,SpreadY = GenerateSpread(), GenerateSpread()
local NewDestination = DestinationPos + Vector3.new(SpreadX, SpreadY, 0)
local rayCast = Ray.new(StartPos, (NewDestination - StartPos).Unit * 100000000000000)