Roblox broke this script and I cant figure out how to fix it

This is a script for the sword fights on the heights randomly disappearing platforms

local plates = {}
local states = {}
local colors = {}


function Init()
	local c = script.Parent:GetChildren()

	for i=1,#c do
		if (c[i].Name == "PhantomPlate") then
			plates[i] = c[i]
			states[i] = 0
			colors[i] = plates[i].BrickColor
		end
	end
end

function UpdatePlate(i)
	
	if (states[i] ~= 0) then states[i] = states[i] + 1 else
		if(math.random(1,75) == 5) then states[i] = 1 end
	end

	if (states[i] < 4) then
		plates[i].Transparency = 0
		plates[i].CanCollide = true
		plates[i].BrickColor = colors[i]
	end

	if (states[i] == 4) then
		plates[i].BrickColor = BrickColor.new(26)
	end
	if (states[i] == 5) then
		plates[i].Transparency = .2
	end
	if (states[i] == 10) then
		plates[i].Transparency = .4
	end
	if (states[i] == 15) then
		plates[i].Transparency = .6
	end
	if (states[i] == 20) then
		plates[i].Transparency = .8
	end
	if (states[i] == 25) then
		plates[i].Transparency = 1
		plates[i].CanCollide = false
	end

	if (states[i] == 50) then
		states[i] = 0
	end
end


function Update()
	for i=1,#plates do
		UpdatePlate(i)
	end
end


function onChildAdded(child)
	if (child.Name == "Trigger") then
		child.Parent = nil
		for i=1,#plates do
			states[i] = 3
		end
	end
end

Init()

script.Parent.ChildAdded:connect(onChildAdded)

while true do
	Update()
	wait(.2)
end

it keeps displaying the error
image

if anyone knows how to fix this to make it work again that would be super helpful

You must have plates as descendants of the script’s parent, and they must all be called “PhantomPlate” for the script to work.

It looks like it’s coming from this line:
if (states[plateNum] ~= 0) then states[i] = states[i] + 1 else
That means that states[I] doesn’t exist, so it is nil and that is why you are getting an error.
So that would mean that states and plates don’t have the same number of elements.
As for why that is happening, my best guess would be that a plate gets added after the init function has been called, which could result in the plate table being expanded, but the states don’t. However, I am not sure, as I don’t know the purpose of the script or if you have another script that could have an impact on it. The best bet would be to add a few print statements to see what is happening to the tables.

The problem is the if statement here. You check if states[i] is not equal to zero yet you don’t check if states[i] is equal to nil. This means that this will return true even if the value is nil, which causes the code to move onto the next line and try to add 1 to nil. You should write:

if states[i] and (states[i] ~= 0) then states[i] = states[i] + 1 else

If this doesn’t work then could you provide a screenshot of the explorer immediately surrounding the script? (e.g: Descendants of the script’s parent)