How do I get the amount of instances in a table?

ugh the inconsistency is caused by spread

toned down spread to 0.1, it’s still inconsistent
toned down spread to 0.01, it’s still inconsistent

toned down spread to 0, it’s consistent

any spread value above 0 breaks the raycasts for some reason :sob:

It’s because of how you’re spreading the direction:

local direction = (mouse.Direction.Unit + Vector3.new(spreadX, spreadY)).Unit

You should be spreading the ray relative to the mouse direction. You’re just changing the direction according to global space and not the mouse direction’s local space, which is why it’s inconsistent.

Create a CFrame at the mouse position in the world and point it in the direction of the mouse, and then move it along its local X and Y axes.

local direction = CFrame.lookAt(endPosition, endPosition + mouse.UnitRay) * CFrame.new(spreadX, spreadY, 0)
1 Like

Also, small side note, there’s a better way to get a random decimal value in between two numbers.

Instead of this:

local spreadX = HORIZONTAL_SPREAD.min + (math.random() * (HORIZONTAL_SPREAD.max - HORIZONTAL_SPREAD.min))
local spreadY = VERTICAL_SPREAD.min + (math.random() * (VERTICAL_SPREAD.max - VERTICAL_SPREAD.min))

Do this:

local ran = Random.new()

local spreadX = ran:NextNumber(HORIZONTAL_SPREAD.min, HORIZONTAL_SPREAD.max)
local spreadY = ran:NextNumber(VERTICAL_SPREAD.min, VERTICAL_SPREAD.max)

I’ve switched from using math.random to the Random object, which provides more capabilites than math.random does.
Read up on it here.

1 Like

i’ll look into this once i’m done with rewriting the entire gun system, thanks