Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with variables?

I was trying to print the names of the models in my set that I have by using GetUserSets method but I keep getting a error saying: Workspace.Script:8: attempt to index global 'Model' (a nil value) Why is it showing vairble Model as nil?

InsertService = game:GetService("InsertService")
UserSets = InsertService:GetUserSets(1077607)
Set = UserSets[1] -- Think of this like a tablet Set 1 = 1 Set 2 = 2 and so on
SetId= Set.AssetSetId
Collection = InsertService:GetCollection(SetId)
for i = 1,10 do
Model = Collection[i]
AssestId = Model.AssestId
print(AssestId.Name)
end

1 answer

✓ Accepted answer

So, to begin - trust Lua. It is nil. You can see this yourself if you printed Model. You'd also see something useful, something like this:

1203523
10358234
1203481
nil
Workspace.Script:8: attempt to index global 'Model' (a nil value)

The reason is probably that your result, Collection is a list fewer than 10 things in it. But, you ask for all the way up to Collection[i]. Asking for an index which isn't in a table will result in nil.

Instead, go up to the length of the table:

for i = 1, #Collection do

3 comments

1

Thanks for the help,but now it won't print any of the names why?

2

assetId is a number, so assetId.anything doesn't make sense. To get the name, use model.name not assetId.Name

1

As an addenum, kevinnight, I suggest looking up generic for loops, which are designed for iterating through tables, like you do here.

This discussion is preserved as it was. New answers, comments, and votes are closed.