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)
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)
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)
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.
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.
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)