I’m trying to get a folders children individually and I’m using for _, PlacedObject in pairs(Plot.PlacedObjects:GetChildren()) do
But it doesn’t seem to run so should I use ipairs instead and even is the difference?
I’m trying to get a folders children individually and I’m using for _, PlacedObject in pairs(Plot.PlacedObjects:GetChildren()) do
But it doesn’t seem to run so should I use ipairs instead and even is the difference?
There’s no true difference between pairs and ipairs except that ipairs will iterate through a dictionary and stop running if an index is nil, however it will run through the indexes in order. That is the main difference, as pairs will not always iterate in order. For example…
local t = {
[1] = "Hello",
[3] = "World!"
}
for i, v in ipairs(t) do
print(i, v) --> "Hello"
end
for i, v in pairs(t) do
print(i, v) --> "World!" "Hello"
end
However, to solve your problem, make sure there’s actually children, or else the pairs loop wont run.
What if first the Placed Objects has no children but when my function restarts that for i loop will it work or no. Because its a build system so when you join there are no children until you place stuff
Well, you’d have to do it every time a child is added. For example
local function childAdded()
for i, v in pairs(folder:GetChildren()) do
--do stuff
end
end
folder.ChildAdded:Connect(childAdded)
local function Save(plr)
local Key = "plr-"..plr.UserId
if Plot then
local save = {}
print("SAVE = {}")
for i, Obj in pairs(Plot.PlacedObjects:GetChildren()) do
print("Attempt Save")
if Obj then
print("Attempt table.insert")
table.insert(save, {
ObjectName = Obj.Name,
CFrames = {
X = Obj.PrimaryPart.CFrame.X,
Y = Obj.PrimaryPart.CFrame.Y,
Z = Obj.PrimaryPart.CFrame.Z,
Orientation = Obj.PrimaryPart.Orientation.Y
}
})
end
end
for _, Saves in pairs(save) do
for _, a in pairs(Saves) do
print(a)
end
end
local success, err = pcall(function()
PlotSave:SetAsync(Key, save)
end)
if not success then
warn("Plot Data failed to save: "..err)
return
end
else
GetPlot(plr)
end
end
You see how this is a function, if the function is fired will it still restart the loop. The print(“attempt save”) doesn’t fire
Basically, this man has the answer.
Hey you helped me on the other post… i added some prints and one part of the script nevers prints:
local function Save(plr)
local Key = "plr-"..plr.UserId
if Plot then
local save = {}
print("SAVE = {}")
for i, Obj in pairs(Plot.PlacedObjects:GetChildren()) do
print("Attempt Save")
if Obj then
print("Attempt table.insert")
table.insert(save, {
ObjectName = Obj.Name,
CFrames = {
X = Obj.PrimaryPart.CFrame.X,
Y = Obj.PrimaryPart.CFrame.Y,
Z = Obj.PrimaryPart.CFrame.Z,
Orientation = Obj.PrimaryPart.Orientation.Y
}
})
end
end
for _, Saves in pairs(save) do
for _, a in pairs(Saves) do
print(a)
end
end
local success, err = pcall(function()
PlotSave:SetAsync(Key, save)
end)
if not success then
warn("Plot Data failed to save: "..err)
return
end
else
GetPlot(plr)
end
end
The print("attempt save)
Now that’s becoming off-topic, and is not on the goal of this topic. You should instead try to solve the issue you want here, by either making a topic, or by “reviving” that one topic you made.
If you are iterating through the results of a call to GetChildren
, you can use ipairs
, since GetChildren
will always return an array.