Pairs loop only runs code on one model

Hi, developers!

I was just here the other day figuring out some other issue and now I’m back!
Today I was experimenting with running multiple stands at once, as seen here:

And when testing a script that initiates a module function through every stand, here:

local folder = script.Parent
local repstorage = game:GetService('ReplicatedStorage')
local autoupdate = require(repstorage.Modules.AutoUpdate)

for i, podium in pairs(folder:GetChildren()) do
	if podium:IsA('Model') then
		print(podium.Name)
		autoupdate.setup(podium)
	end
end

… my console only showed that it picked up one podium.
Not sure if this happens because of modules and running functions, but this certainly wasn’t what I was expecting.
image
image

(Note that it doesn’t say ‘ItemPodium x2 - Server’ or whatever, despite there being two podiums.)

Any help would be hugely appreciated. My own theories is that it won’t run both because they’re named the same thing, but I digress.

1 Like

Late addition, but here’s the output of the code and all that jazz:
image

How the module looks like?ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

Here’s the full module. Let me know if this is what you need!

Try this, just for debugging:

local folder = script.Parent
local repstorage = game:GetService('ReplicatedStorage')
local autoupdate = require(repstorage:WaitForChild("Modules"):WaitForChild("autoupdate"))

for i, podium in pairs(folder:GetChildren()) do
	if podium:IsA('Model') then
		print(podium.Name)
		spawn(function()
			autoupdate.setup(podium)
		end)
	end
end

It loops through the podiums successfully, but apparently gets the information of the last podium and sets it for all of them.

image

Podium on the left should be configured to Dominus Empyreus

I see, well, I think the issue is on how the module is handling the incoming Model that you are sending on each iteration, I would need to carefully check the module to find the issue.

1 Like

I just tried checking the item info it’s collecting and its name and it prints the same thing for both podiums, so I’ll try to figure out what’s happening.

Update: found the issue! The items display were being shown pre-emptively, before even displaying the other properties of the item, so it would get the Empyreus first, then the fast food hood, but since the fast food hood came second, it would override the Empyreus display.

	local item = iService:LoadAsset(assetID_s2)

		for _, v in pairs(item:GetDescendants()) do
			if v:IsA('SpecialMesh') and not table.find(used, v.MeshId) then
				intTex = v.TextureId
				intMesh = v.MeshId
				table.insert(used, v.MeshId)
				item:Destroy() -- cleanup if it hasnt already
			end
		end

Super ugly code, but I can’t figure out a better way, even with tables. At least it works, but it’ll probably cause some performance issues with spamming the inserted item.

After a quick check of the module I think I found the reason.

You are storing the ID of the mesh and Texture on a gobal variable on the module. Meaning that a new call of the module function that variables are replaced with the last item ID you sent from the iteration, and when using the “auto-update” while loop, all the previous models gets updated with the last variable you stored

I considered using a table called ‘used’ to store the already used Mesh ID’s, but it came with the same issue of overriding. The solution I found above is, albeit, temporary because of its possibility to ruin performance. I can definitely look into this issue more, though!

I really dont think thats the root of the issue, thats only a way in between of the real issue, which I explained in my previous message.

I think what I said is the root issue, specially cause the idea of using a Module is the ability to use it as an external way to create “classes” so you are not depending on hardcoded variables, and it becomes dynamic. And that purpose kinda gets lost when you are using it in this way

Should I store the texture and mesh ID of the specified display item in attributes or values stored in the seperate podium items instead?

I think that the variables should be local to the module’s thread that were created when it been called. Encapsulated inside the thread and only existing inside that thread.
Seems, Im not pretty good at explaining… lol

2 Likes

Well, it’s more or less that I’m struggling to understand what you’re trying to tell me :pensive: I can’t say that I’m a hugely avid programmer, and albeit my solution worked, it’s skyrocketing memory usage to ~2.000 MB. I can try to dissect what you’re explaining to me, but I’ll probably need clarification if I can’t figure it out.

1 Like

When i get home I can try to explain it using your code. Peashie definitely nailed the issue though confused me for a moment lol. Its where your constructing the variables and where your using them are you current issue.

However may I recommend using “CollectionService” and Tags

You could apply a tag to a item after its spawned and being displayed and cross reference check when spawning a new item to ensure you never show multiple items at the same time.

You could also add a rarity system with tags so more commons display over others ect. Also I hit some awkward memory usage and I am unsure if roblox is just bugged. Also note studio increases memory usage. I was actually gonna make a topic tonight to discuss this issue because it threw me on a 5 hour investigation last night

Yeah, I’m completely lost… I have no idea where to place the variables!!