I need help in table it is not indexing properly

i was trying to make a plot system but it always index randomly

also print(workspace.Plots:GetChildren()[1]) gave plot 2
Capture

snip
and table sometime start with 8 sometime with 4 etc

local PS = script.Parent:WaitForChild("plot")
local Frame = PS:WaitForChild("SelectPlotFrame")
local left = Frame:WaitForChild("LeftArrow")
local right = Frame:WaitForChild("RightArrow")
local select1 = Frame:WaitForChild("SelectPlotButton")

local selectedplot = Frame:WaitForChild("Selectedplot")

local plots = game.Workspace.Plots

local function FindunOwnedplot ()
	
	local avPlot = {}
	for i,plot in ipairs(plots:GetChildren()) do
		
		if plot.owner.Value == nil then
			
			table.insert(avPlot,plot)

		end
	end
	return avPlot
end

local plotstable = FindunOwnedplot()

print(plotstable)
local index = 1
selectedplot.Value= plotstable[1]
print(selectedplot.Value)
right.MouseButton1Up:Connect(function()
	
	if plots:FindFirstChild("Plot"..index - 1 ) then
		index -= 1
	else
		index = 9
	end
	selectedplot.Value = plotstable[index]
end)

left.MouseButton1Up:Connect(function()
	
	if plots:FindFirstChild("Plot"..index + 1) then
		index += 1
	else
		index = 1
	end
	selectedplot.Value = plotstable[index]
end)
```can someone please tell what is a problem
1 Like

GetChildren does not necessarily return the children in the order they appear in the explorer window. You’ll manually have to order them, e.g. using table.sort.

1 Like

yea you are right it is because of the get childern

Since you are using an i loop you can do this to get it in order.

local plots = workspace.Plots

for i = 1, #plots:GetChildren() do
    local plot = plots:FindFirstChild("Plot"..i)

    if plot then --just in case it doesn't exist for whatever reason
        --do something with plot
    end
end
1 Like

it work thx

for i = 1, #plots:GetChildren() do
		local plot = plots:FindFirstChild("Plot"..i)
		if plot.owner.Value == nil then

			table.insert(avPlot,plot)

		end
	end
	return avPlot
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.