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
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
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.
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.
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.
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
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(