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

Can I see how you’re spawning the npcs?

function spawnNpc()
    npc = NPCsFolder.NPC:Clone()
    generateNPCApearance(npc)
    npc.Parent = workspace.Map.NPCs
end

when you do npc = NPCsFolder.NPC:Clone() you’re not making it a local variable to only that function. Does it need to be global or can you change it to local? I think changing to local may fix the issue.

Well, this is the full code. I also have a random chance for spawning VIP NPCs but that isn’t very relevant to the appearance of normal NPCs so I cut it out. Here is the full function in my code:

function spawnNpc()
	local isVIP = math.random(1,chanceOfVIP)
	local npc = nil
	
	if isVIP == 1 then
		npc = NPCsFolder.VIP:Clone()
		npc.Parent = workspace.Map.NPCs
		
	else -- spawn normal NPC
		npc = NPCsFolder.NPC:Clone()
		
		generateNPCApearance(npc)
		
		npc.Parent = workspace.Map.NPCs
		
		wait(1)
	end
end

Also code for spawning them:

for i = 1,EmptySpotsAtDesk,1 do
	spawnNpc()
end

Here is my final idea if this doesn’t work then I think settling with wait() will have to be the way to go for now. Use coroutine create like below in the for loop. And function spawnNpc() should be made local. local function spawnNpc()

for i = 1,EmptySpotsAtDesk,1 do
	coroutine.resume(coroutine.create(spawnNpc)
end

Unfortunately it does not work :frowning:

Also wait() is too short, I noticed that anything less than around .2 is too fast. I’ll keep trying to think of a solution, thank you for helping me :slight_smile:

unfortunate. for debugging could you add a print(math.random(1,9)) or use one of the tables, to see if the math.randoms are returning the same values over and over for each npc or if they’re not running the correct amount of times.

I tried the math.random(1,9) AND THEY PRINT THE SAME NUMBER EVERTIME! THE LAST 2! ITS DRIVING ME NUTS!

I don’t understand how the function can be running for 2 NPCs…

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