I am fairly new to scripting in general and even more so to tables.
I am trying to figure out if there is a for i, v in pairs (table:GetChildren() do alternative that will actually work for tables.
Basically I need to be able to get each of the values that are inside of a table and do run a function for each different value.
So far I know that GetChildren() does not work because a table is not an instance. I have also tried table.foreach and table.foreachi as I would have assumed that these would have worked each value within the table, but this is the result of that:
With only one value, I noticed it continuously added extra characters to the value:
With multiple values, I realized that it was just taking the entire table, and running the function or that:
The function is supposed to be ran on each value, and is supposed to index them with the in-game values.
Basically, I am just looking for a GetChildren for tables. If anyone can help me with this, please do! Thank you!
1st of all, WOW I really did not know that, so thank you!
2nd, now my code is only running up to my first table. Here is a section of my code, where you can see print functions. This won't print obviously does not print, and I am assuming that is because the loop from the first section/table does not stop running, even though there is only 1 value with that table.
Summary
print("this will print")
local anims = {}
table.insert(anims, stored[7])
if #anims > 0 then
print("passed anim check 1")
for i, v in pairs(anims) do
print(v)
if v == "[]" then return end
local item = animationsinventory:FindFirstChild(v)
if item then
print(item) -- does not print because v does = []
item.Value = true
else
print("created anim item")
local bool = Instance.new("BoolValue")
bool.Name = v
bool.Value = true
bool.Parent = animationsinventory
end
end
end
print("this wont print")
local effects = {}
table.insert(effects, stored[8])
if #effects > 0 then -- none of this happens
for i, v in pairs(effects) do
if v == "[]" then return end
local item = effectsinventory:FindFirstChild(v)
if item then
item.Value = true
else
local bool = Instance.new("BoolValue")
bool.Name = v
bool.Value = true
bool.Parent = effectsinventory
end
end
end
this might be your problem, return will end the entire function this is in
example
local hello = nil
local function func()
local t = {}
table.insert(t,hello)
if #t > 0 then
for _, v in pairs (t) do
if not hello then return end -- as hello is nil this will return ending the function
end
end
print("this will never print") -- as the function as been returned before this point this will never print
end
func()
I get no errors related to the saving/loading of data, but I believe that I might be having an issue with saving my tables via JSONEncode-ing.
I have not been JSONDecode-ing any of the tables as I honestly did not know anything about it; I just seen that encoding was on the only way to save tables didn’t think about having to decode them after.
I am currently having an issue where this code:
Summary
local phones = {}
table.insert(phones, stored[9])
print(phones)
if #phones > 0 then
for i, v in pairs(phones) do
print(v)
if v == "[]" then continue end
local item = phoneinventory:FindFirstChild(v)
if item then
item.Value = true
print(item)
else
local bool = Instance.new("BoolValue")
bool.Name = v
bool.Value = true
bool.Parent = phoneinventory
end
end
end
Via the print functions, prints each saved Value as one object.
Print(v):
Print(table):
I am assuming that this is being rooted in the fact that I have not been decoding the tables?