Getting more then 1 value from a folder

My problem is that I have a folder in ReplicatedStorage that has values. I am trying to get those values from a local script and checking them in a textbox to see if they match. But I am just getting nil when I try to print the values. This is in a GUI

My Script

script.Parent.MouseButton1Click:Connect(function()
	wait(2)
	local Vals = game.ReplicatedStorage.Vals:GetChildren()
	print(Vals.Value)
	if script.Parent.Parent.Parent.Parent.Value.Value == Vals.Value then
		print("True")
	end
end)

Another Variant

script.Parent.MouseButton1Click:Connect(function()
	wait(2)
	local Vals = game.ReplicatedStorage.Vals:GetChildren()
	for _, Card in pairs(Vals) do
		print(Vals.Value)
		if script.Parent.Parent.Parent.Parent.Value.Value == Vals.Value then
			print("True")
		end
	end
end)

	


The issue is that :GetChildren() returns a Table, not one single object. Example:

print(workspace:GetChildren()) --> {Baseplate, Part1, Part2, Sound}

Just loop through the result of :GetChildren() to get each value object

Alright, I will try that

(characters)

1 Like

Still getting nil from the values

Can you show the current code you edited?
charssssssssssss

local num = 0
script.Parent.MouseButton1Click:Connect(function()
	wait(2)
	repeat
		local Vals = game.ReplicatedStorage.Vals:GetChildren()
		print(Vals.Value)
		if script.Parent.Parent.Parent.Parent.Value.Value == Vals.Value then
			print("True")
		end
		num += 1
	until num == 10
end)

image

You did not loop through the table like I said at all

for value_obj in pairs(game.ReplicatedStorage.Vals:GetChildren()) do
   print(value_obj.Value)
end

Oh sry, trying to fix a thing and it’s 4 AM. Will do that now. Sorry again

Just get an error

Could you show the edited code with each reply? Makes it easier and requires less back and forth communication, sorry for not specifying earlier.

No worries,

script.Parent.MouseButton1Click:Connect(function()
	wait(2)
	for value_obj in pairs(game.ReplicatedStorage.Vals:GetChildren()) do
		print(value_obj.Value)
		if script.Parent.Parent.Parent.Parent.Value.Value == value_obj.Value then
			print("True")
		end
	end
		
end)

Line 6 is the Print

For pairs, the first value is the numerical index of the table and the second is the object

script.Parent.MouseButton1Click:Connect(function()
	task.wait(2)
	for Index, value_obj in pairs(game.ReplicatedStorage.Vals:GetChildren()) do
		print(value_obj.Value)
		if script.Parent.Parent.Parent.Parent.Value.Value == value_obj.Value then
			print("True")
		end
	end
end)

in this case it is numeric, for mixed tables or dictionaries it may vary.

1 Like

Alright, Will try that

Charsss

It worked, thanks so much!

Have a good day/morning/night

1 Like
local storage = game:GetService("ReplicatedStorage")
local vals = storage:WaitForChild("Vals"):GetChildren()

script.Parent.MouseButton1Click:Connect(function()
	task.wait(3)
	for _, value in ipairs(vals) do
		print(value)
		if script.Parent.Parent.Parent.Parent:WaitForChild("Value").Value == value then
			print("True!")
		end
	end
end)

I believe this is what you want. I’ve made a few optimisations, ipairs is faster than pairs, “GetChildren()” is only performed once when the script first executes, I’ve also added wait commands to allow for instances to load before attempting to use them in the script.

If children are later added, the variable wouldn’t contain them

I’m under an educated assumption that they’re not being added, if they are then it would be more preferable to use ChildAdded/DescendantAdded to indicate so.

local storage = game:GetService("ReplicatedStorage")
local vals = storage:WaitForChild("Vals")
local values = vals:GetChildren()

vals.ChildAdded:Connect(function()
	values = vals:GetChildren()
end)

script.Parent.MouseButton1Click:Connect(function()
	task.wait(3)
	for _, value in ipairs(values) do
		print(value)
		if script.Parent.Parent.Parent.Parent:WaitForChild("Value").Value == value then
			print("True!")
		end
	end
end)