Math.random duplicates

I’m trying to make another shotgun spread, and I’ve fixed the spreading directions, however sometimes the shotgun fires in a straight line. This is because of a math.random duplicate This is the output

Part 7.68883085, 3.25965953, 11.7818241 (x2)
  Part 7.68883085, 3.25965977, 11.7818241 (x2)
  nil 22.2675266, 16.2970352, -36.9516144
  Part 6.14408875, 4.21448708, 8.80914402 (x9)

How do I fix this?

1 Like

Show us code. We can’t immediately fix something which could have thousands of solutions narrowed down to one

My bad. Here it is

		local dir = CFrame.new(mainpos, mousehit) * CFrame.Angles(math.random(-spread,spread)/15,math.random(-spread,spread)/19,0)

Sorry about that!

1 Like

This could be because of the nature of how math.random() works. I suggest you try using Random, specifically Random:NextNumber(), instead, and seeing if that makes a difference.

1 Like

Curious as to what the 15 and the 19 significance in the script is, also is that in another portion of the script?

1 Like

Here’s what I did now: Also the division is for the spread so that it doesn’t go all over the place

	local x = Random:NextNumber(-spread,spread)/15
	local y = Random:NextNumber(-spread,spread)/19
	local dir = CFrame.new(mainpos, mousehit) * CFrame.Angles(x,y,0)

When I use Random it says:
attempt to call a nil value
what do I do?

Are there any loops that might be iterating twice?

1 Like

no, only a simple for loop that goes
for i = 1,10 in pairs do

I figured out why.
It’s supposed to be like this:

Random.new():NextNumber()

i didn’t have the .new() part

1 Like