Need help with indexing arrays

im trying to make a bomb script that every tick bomb it changes color and turn to neon

--Credit to GFink, i tried changing bits but no succses
                local ui = 0.7 -- updateInterval  --
		local c,m = 1,1 -- currentMat
		local colors= {"Really black","Bright red","Electric blue","Deep orange","New yeller"}
		local materials = {"SmoothPlastic","Neon"}
		
        local ticksound = Object.Click

		local function ud() --update func
			ui = ui * 0.9 --0.9
			Object.BrickColor = BrickColor.new(colors[c])
			Object.Material = materials[m] 
			c = c+1
			m = m + 1
			if c>5 or m>2  then -- got confused here cus i dont know what to do im trying to make if colors reached its length of 5 and materials 2 then both should change to 1 but errors "Attempt index with nil" pls help :(
				c = 1
                m = 1 
   			end
		end

		while ui > .1 do --.1
			Wait(ui)
			ud()	
			local l=ticksound:Clone()
			l.Parent=Object
			l:Play()
		end

For times like this, I suggest using the lua debugger to monitor the values of c and m in order to see if they are indexing the table out of bounds which is causing the error.

Also which line is exactly causing the error is it this one?

or this one?

materials. sorry for long reply but when run the game it says attempt to index material with nil idk maybe because its indexing by the color but idk how to seperate both arrays

I tested out the indexing it works but it doesn’t fully cycle through all the colors because both index values are reset to 1. I suggest separating the if statements to only reset once the index is out of the table bounds like so using # length operator.

if c > #colors then
	c=1
end
if m>#materials then
	m=1
end

right now the code cycles through this

--(c,m)
1,1
2,2
1,1
2,2
...

Other than that what is the Object variable?

Object is supposed to refer to the bomb I believe and change its materials property. Did you make sure to define Object as the bomb base part?

1 Like

yes the object is the bomb Part, hey it works now thanks :slight_smile: