How can I prevent the NPCs from having the same outfits?

I even went to the extreme and had them do this:

warn(math.random() * (math.random(1,100) / (math.random(1,43) ) ) )

My output:
9.5129638149295
14.431920772682
14.431920772682

Yeah, the problem is the math.random seed is updating between the first and second prints but not between second and third so it gives the same output for those two. You can force the math.random seed to update with the function math.randomseed(tick()). try it out.

This is so weird.

Even after doing this:

function generateNPCApearance(npc)
	-- blah blah code from before
	
	warn(math.random() * (math.random(1,100) / (math.random(1,43) ) ) )
	math.randomseed(tick())
end

They still print the same number. Its like they are clones!

Alright, did more debugging to find out that the function is indeed running 3 times. That is what is expected. Its just like the last two functions are perfectly the same and they are clones or something.

stop setting the seed everytime it is called. not your issue but you’re creating a new one.

I know. I did that just to see if the math.random would print anything different. Spoiler alert, it did not. Do you have any ideas as to why this could be happening?

That’s incredible. Hmm, you could try creating new rng table at the start of the function and avoid math.random entirely and instead choosing to use :NextNumber(). Here is the page for it A Random Feature - Updates / Announcements - DevForum

function generateNPCApearance(npc)
    rng = Random.new()
	-- blah blah code 
    print(rng:NextNumber(0,9))
end

It. Prints. Different. Numbers!
Is this able to help us in some way?

Yeah, switch out all the math.randoms you use with the rng:NextNumber(), should make the npcs not repeat anymore.

Ive never used Random.new() and :NextNumber() before, could you maybe show me how to do it with this stuff?

local shirt = shirts_folder[gender]:GetChildren()[math.random(1,#shirts_folder[gender]:GetChildren())]:Clone()
local pants = pants_folder[gender]:GetChildren()[math.random(1,#pants_folder[gender]:GetChildren())]:Clone()
local bodyColor = bodyColors[math.random(1, #bodyColors)]:Clone()
local hair = hairs[gender]:GetChildren()[math.random(1,#hairs[gender]:GetChildren())]:Clone()
local shirt = shirts_folder[gender]:GetChildren()[rng:NextNumber(1,#shirts_folder[gender]:GetChildren())]:Clone()
local pants = pants_folder[gender]:GetChildren()[rng:NextNumber(1,#pants_folder[gender]:GetChildren())]:Clone()
local bodyColor = bodyColors[rng:NextNumber(1, #bodyColors)]:Clone()
local hair = hairs[gender]:GetChildren()[rng:NextNumber(1,#hairs[gender]:GetChildren())]:Clone()

Really simple, rng:NextNumber( in place of math.random(

Now Ive got an error with this method:

ServerScriptService.SpawnNPCs:26: attempt to index nil with 'Clone'

Btw this is for cloning the shirt, I guess it can’t find one using :NextNumber()?

Continued to debug. Found out its looking for an item in position 3 with a lot of decimals. Should I use :NextInteger() since it is whole numbers?

yes, im so sorry, use nextinteger so it returns whole numbers.

3 Likes

IT WORKS!!! TYSM!

Im literally so happy rn, I couldn’t finish my NPC system without you! :grin:
I can’t thank you enough.

4 Likes