For loop shows every other index as nil

Hi, I have recently been trying to fix an issue that i can’t see to find any posts about. The issue is that when ever a round ends in my game the round handler fires a function on the stage handler to clear all the stages in the “generatedStages” table.

When looping through the table in the stage handler and printing the values each value is not nil
When looping through in the destroy module, every other value is nil.

Stage loader script
--> Services <--
local ServerStorage = game:GetService("ServerStorage")
local WorkspaceService = game:GetService("Workspace")

--> Modules <--
local modules = ServerStorage.Modules
local utilModules = modules.Utilities
local behaviourModules = script:GetChildren()
local stageUtils = utilModules.Stage_Utils

local applyBehaviours = require(stageUtils.ApplyBehaviours_StageUtil)
local destroyStage = require(stageUtils.Destroy_StageUtil)
local getRandomDirection = require(stageUtils.GetRandomDirection_StageUtil)
local getRandomStage = require(stageUtils.GetRandomStage_StageUtil)

--> Variables <--

local workspaceStagesFolder = WorkspaceService:WaitForChild("Stages")

local stageLoader = {}
stageLoader.generatedStages = {}
stageLoader.amountGenerated = 0

local viewStageConnections = true
local stageConnectionTransparency = 0.4

--> Methods <--

local function showParts(partTable : { Part }, transparency : number)
	if not partTable then return end

	for _, stagePart in pairs(partTable) do
		
		if not stagePart then continue end
		if not stagePart:IsA("Part") then continue end
		
		stagePart.Transparency = transparency
	end
end

function stageLoader.create(stageInfo)
	
	-- Check for valid stage info
	
	if not stageInfo then return end
	if not stageInfo.difficulty then return end
	
	-- Define stage info
	-- Get stage model and position
	
	local difficulty = stageInfo.difficulty
	local stage = getRandomStage(difficulty)
	local position = getRandomDirection(stageLoader.generatedStages)
	
	if not stage then return end
	if not position then return end
	
	local stageConnections = stage.stageConnections:GetChildren()
	local stageMain = stage.stageMain:GetDescendants()
	
	-- Check for stage main and connections
	-- Check if debug variable is true and there are stage connections then show stage connections
	
	if not stageMain then return end
	if not stageConnections then return end
	if viewStageConnections and stageConnections then task.spawn(showParts, stageConnections, stageConnectionTransparency) end
	
	-- Configure stage properties
	-- Apply behavior modules to the stage
	
	task.spawn(showParts, stageMain, 1)
	stage.Parent = workspaceStagesFolder
	stage.Name = (stage.Name.."_"..tostring(stageLoader.amountGenerated))
	stage:PivotTo(position)
	task.spawn(applyBehaviours, stage, behaviourModules)
	
	stageLoader.amountGenerated += 1
	
	-- Append stage into generated stages

	stageLoader.generatedStages[#stageLoader.generatedStages + 1] = stage
	
	return stage
end

function stageLoader.clear()
	
	for i=1, #stageLoader.generatedStages do
		print(stageLoader.generatedStages[i])
	end
	
	task.spawn(destroyStage,stageLoader.generatedStages, stageLoader.generatedStages)
	
	table.clear(stageLoader.generatedStages)
end
	
return stageLoader

Destroy script
--> Services <--
local Debris = game:GetService("Debris")

--> Variables <--

local minVelocity = -100
local maxVelocity = 100
local unAnchorWaitTime = 1
local debrisTime = 10
local destroyTag = "destroying"

--> Methods <--

return function (stages : { Model }, genStages : { any })

	-- Check for valid params
	
	print(stages)	

	if not stages then return end
	if not genStages then return end

	-- Loop through every stage in the table

	for _, stage in pairs(stages) do
		
		print(stage.Name)
		
		-- Remove the stage from the generated stages

		table.remove(genStages, table.find(genStages, stage))

		-- Apply a destroy tag for the client to handle
		-- Add the stage model to debris to be garbage collected

		stage:AddTag(destroyTag)
		Debris:AddItem(stage, debrisTime)

		-- Loop through each part to unanchor and apply a random velocity

		task.spawn(function()
			task.wait(unAnchorWaitTime)

			for _, stagePart in pairs(stage:GetDescendants()) do

				-- Check for a valid part

				if not stagePart:IsA("Part") then continue end
				if stagePart:HasTag("dontTween") then continue end

				-- Get a random velocity


				local randomVelocity = Vector3.new(math.random(minVelocity, maxVelocity), 0, math.random(minVelocity, maxVelocity))

				stagePart.Anchored = false
				stagePart.CanCollide = false
				stagePart.Massless = true
				stagePart.AssemblyLinearVelocity = randomVelocity
				stagePart.AssemblyAngularVelocity = randomVelocity

			end
		end)
	end
end

Video of the issue when only every other stage is destroyed

Sorry, but I can’t quite pinpoint what your root issue is. From what I understand, you are encountering a problem where a for loop attempts to show the key’s value but it instead shows nil, right? Can you please highlight the for loop code block that shows this issue in your script so that we can better identify the issue?

Ok, so its seems fixed now, because of the table.remove(genStages, table.find(genStages, stage)) was doing something to the value but i have no idea why.

Might be because of the location of the table.remove()

Edit: The table.remove was the issue so i just removed it with genStages[i] = nil

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