Random Number function Not working

I made a post about it before but i tried to simplify it and i think it confused a few people so im going to close it and redo this because its really annoying

So In short, I’m trying to create Raider NPCs but I need there hair and gender random.

I’ve got this code below that is meant to do it
But whenever it runs. the first, NPC has random hair and gender As well as the second
But the 3rd+ are all replications of the 2nd NPC
if I spawn 100 NPCs the first 2 are random and the other 98 are the same as the second

function Raid()
	
	--Creating Raiders
	
	for i = 1,3 do
		
		local RandomNumber = math.random(1,2)
		local Gender = nil
		
		if RandomNumber == 1 then 
			Gender = "Male"		
		else
			Gender = "Female"
		end
		
        Raider = game.Lighting:FindFirstChild(Gender):Clone()
		Raider.Parent = game.Workspace.Raiders
		Raider:MoveTo(game.Workspace.SpawnPoint.Position)
		Raider.Name = "Raider"

		local RandomNumberTwo = math.random(1,12)
		local Hair = game.Lighting.Hair:FindFirstChild(Gender.. "-".. tostring(RandomNumberTwo)):Clone()


	end 

end

Raid()

The stupidest part of this problem is that if i remove the line:

Raider.Parent = game.Workspace.Raiders

Works completely fine, it makes no god damn sense

(Ive also put “RandomNumber = 255” print(RandomNumber ), just to check if the Variable stays the same from the last loop through but no, the Random number generator genrally generates the exact same number. )

(Solutions ive already tried:

  1. Localising RandomNumber Variables outside the function instead of inside
  2. removing the FOR loop and running the function multiple times instead
  3. Restarting Studio
  4. A ton of code rearranging
    )

Please help this is a problem ive been stuck with for 3 days and i know its something really stupid thats making this script not work

1 Like

For i = 1 to 3 do

Isn’t’ that your issue, the script only runs 3 times.

1 Like

Na, if I run it 100 times (I=1,100). The 98 NPCs after the second have the same hair and gender and the 2nd one :confused:

1 Like

Why are you using Lighting to store your variables?
If that ‘FindFirstChild(Gender)’ value isn’t changing then maybe it’s storing a load of values there, but only reading the first one it finds?

Gender is not a variable, it’s the NPC model.

And I know it’s not the retrieval of the NPC because I’ve used print functions after the number generating lines, so I know it’s the function and not the other part of the code.

Sorry, that’s what I meant.
If you store the model in Lighting, but are retrieving FindFirstChild(Gender) and cloning it as the Raider but not Destroying it in Lighting, won’t you get a stack of them there, but only be assigning Raider as the first child the script finds?

Basically I have two Models in lighting

Male - a R15 male dummy
Female a R15 female dummy

The raider part of the code, Clones one, Places the clone in a folder in workspace, Changing there position to where I spawn them and then changing there name to “Raider”

There isn’t a overflowing stack in lighting because I’m moving the Clones into a folder in workspace.

I cant destroy the original model because I need it to create more clones.

I hope this helps understand the problem a bit better

Okay. ive fixed the problem but i dont really understand why

i had a wait() in the function but it didnt do anything, but now i just added wait(1) and it works?

The whole script is in the server so i dont really understand?

can someone explain?

1 Like

I had this exact same problem before when generating NPCs in a for loop. The first two are different, the rest are clones.

Luckily, I found a solution to this problem!

I don’t know exactly why this happens, but math.random just… dies when doing something like this. I fixed it by changing everything from:

math.random(1,12)

to this:

local rng = Random.new()
local randomNumber = rng:NextInteger(1,12)

This should fix your issue. Just replace math.random with rng:NextInteger. If you want to read more about it and how I figured it out, visit this topic I made:

1 Like

It works, But i still just don’t understand why it breaks.

This has probably been the most frustrating thing ever to fix XD

I honestly do not know why either. I had tried printing a pretty complex equation made of math.random and it happened to be the same, every time. I don’t think the random seed updates in time. Glad this fixed your issue, it was a pain to figure out for me and a few others as well :sweat_smile:

1 Like