Comparing children with child added and a for loop Makes my brain melt

a gun has an image id stored
So player picks up a gun from the ground. It goes to his gui. but i want it to display using the image id as the identifier. . but then using a for loop to loop through the children of the gui. with child.Added event. it is going to check for the the child added and compare it the gui children. the thing is when the child is added the for loop runs so it runs through the child that im supposed to check it with the other children. so i end up checking for the same thing( the Child that was added)
Any idea on what to do?

local player = game.Players.LocalPlayer
local WeaponScrollFrame= script.Parent
local Backpack = player:WaitForChild("Backpack")
local folder =player:WaitForChild("CheckFolder")
local QFolder= player:WaitForChild("QFolder")
local WeaponsFolder = player:WaitForChild("WeaponFolder")

folder.ChildAdded:Connect(function(Child)
	for _, Button in pairs(WeaponScrollFrame:GetChildren()) do
		if Button.Name == "ImageButton" then
			if Button.Image == Child.Value.Value then
				print("123")
				break
			elseif Button.Image ~= Child.Value.Value  then
				print(Button.Image)
				print(Child.Value.Value)
				print("123")
				local Buttonfor = game.Workspace.ImageButton:Clone()
				wait()
				Buttonfor.Image = Child.Value.Value
				Buttonfor.Parent=WeaponScrollFrame
			end
		end
	end
end)
  1. What solutions have you tried so far? everything my brain is metling
1 Like

Im confused what your trying to do. Im not sure why your trying to iterate through all the children of the weapon scroll frame every time a new child is added.

I don’t understand what you are trying to convey in this post, a solution I have for you is to replace the for loop that is being called constantly and use tables. You can add and subtract a numerical variable and link that to find the item you have stored within the table.

Create a table where the key is the weapon or the “Child”.
The value in this table should be the name of the image you want to set, or whatever data you need to use for whatever it is you’re trying to do.
Whenever something is added to this “folder”, in your code, you’re trying to check every single item in the folder, when you really only need to make changes to the item that is currently being added. You can use the table to determine what values to assign to the new item in the folder.

I would recommend that you find a tutorial on tables/arrays in Lua, tables will help you solve this problem.

1 Like

but then. for a datastore . how would i save it with a data store

how would i save that table for a datastore?

You can get the datastore, and then just set the new one directly.

wdym? How can i save a table that changes over time inside a local script? to a data store

The thing is, you cannot set data in datastore using LocalScripts. You must use a server script. This was done by Roblox as a preventive measure from exploiters.

i know. im asking how can i save a table that exists inside a local script to a datastore?

Hey. You can fire the table to a server using a RemoteEvent .

Lmao. I tried saving the table with a _G from like 3 days . Then stopped. And spent over 3 hours trying to get the script above to work since i thought the table wont work with a data store. Thanks!

save the table to the datastore! and use UpdateAsync to save the changed elements inside that saved table. you will need to make a table structure to give the players so you can easily save that information in a set table