Math.random() help

I’m trying to randomly pick a hair that’s inside of a folder and cone it onto an NPC but this code won’t work!

Error:
Attempt to get length of a Instance value

Script:

for _, PrimaryHat in pairs(MainFolder[NPCGender.."Appearances"].PrimaryHats:GetChildren()) do
     local ChosenPrimaryHat = PrimaryHat[math.random(#PrimaryHat)]
     ChosenPrimaryHat:Clone().Parent = NewNPC
end
1 Like

Add a Table within the Table, so {PrimaryHats:GetChildren()}

local PHats = MainFolder[NPCGender.."Appearances"].PrimaryHats

for _, Hat in pairs({PHats:GetChildren()}) do
   local ChosenPrimaryHat = Hat[math.random(#Hat)]
    ChosenPrimaryHat:Clone().Parent = NewNPC
end

this will look within a table, with a table inside it

You are getting this error because the for loop is looking through the table, and its finding Instances, and not Arrays

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.