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:
Localising RandomNumber Variables outside the function instead of inside
removing the FOR loop and running the function multiple times instead
Restarting Studio
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
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?
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?
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
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:
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