Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this table skipping a bunch of iterations?

The table positions should include only 100 items, but for whatever reason, includes 107. Upon inspection of the prints, 100 items are added, supposedly at their correct position (their name), but looking at when the table itself was looped through after all items were added, it appears to have added all items through 1-9, but for some reason skipped 10-16, and the next item was at position 17. There don't appear to be any more gaps.

Server script:

local parts = script.Parent:GetChildren()
local del = 0.006 -- Short for "delay", because delay is a function
local positions = {}
local debounce = false

for i, child in ipairs(parts) do
	print(child.Name)
	
	if child == script or child.Name == 'Sort' or child.Name == 'Base' or child.Name == 'Shuffle' then
		parts[i] = nil
	end
end

for i, child in pairs(parts) do
	child.SurfaceGui.TextLabel.Text = child.Name
	child.SurfaceGui.TextLabel.TextColor3 = Color3.fromRGB(255 - tonumber(child.Name * 2.55), 255 - tonumber(child.Name * 2.55), 255 - tonumber(child.Name * 2.55))
	child.Color = Color3.fromRGB(tonumber(child.Name) * 2.55, tonumber(child.Name) * 2.55, tonumber(child.Name) * 2.55)
	
	table.insert(positions, tonumber(child.Name), child.Position)
	
	print(tonumber(child.Name), positions[tonumber(child.Name)] ~= nil)
end

script.Parent.Shuffle.ClickDetector.MouseClick:Connect(function()
	if debounce then return else debounce = true end
	
	local record = {} -- ignore
	
	for i, child in pairs(parts) do
		for I, swap in pairs(parts) do
			local newPos = math.random(1, 100)
				
			if tonumber(child.Name) == newPos and i ~= I then
				local swapPos = swap.Position
				
				swap.Position = child.Position
				child.Position = swapPos
				
				table.insert(record, 'Randomness: '..tostring(math.abs(swap.Name - newPos))..'%, new position: '..newPos) -- ignore

				wait(del)
			end
		end
	end
	
	debounce = false
end)

script.Parent.Sort.ClickDetector.MouseClick:Connect(function()
	if debounce then return else debounce = true end
	
	for i, child in pairs(parts) do
		local partPos = child.Position
		
		print(tonumber(child.Name), positions[tonumber(child.Name)] ~= nil)
		
		child.Position = positions[tonumber(child.Name)]
		
		for I, CHILD in pairs(parts) do
			if CHILD.Position == child.Position and i ~= I then
				CHILD.Position = partPos
			end
		end
		
		wait(del)
	end
	
	debounce = false
end)

1 answer

This discussion is preserved as it was. New answers, comments, and votes are closed.