How to check if value matches table value

I am creating a tower defense game and basing wave strength off of what round that game is on.

Every 5 rounds a boss will spawn. Instead of creating a block of code for every (5, 10, 15, 20… etc.) round the player is on, I created a table of these values.

The idea behind this code is it checks the wave (round) and if the that value is within the table then a boss will spawn.

    elseif wave == table.find(fifthWaves, wave) then
		print("boss wave", wave)
		mob.Spawn("fein", 10 * wave, map)
		task.wait(2)
		mob.Spawn("megafein", 1 * wave, map)
		mob.Spawn("monster", 1 * wave, map)
	
	elseif wave > 5 then

However, this does not seem to work?

that does seem to likely work but maybe try using fifthWaves[wave]

1 Like

I tried adding this, instead of what I had:

elseif wave ==  fifthWaves[wave] then

They both just seem to skip every 5th wave completely

it could be wrong with the wave variable, is it a numberValue?

Here is the full wave handling script:

Summary
for wave=5, 20 do
	print("WAVE HAS STARTED:", wave)
	workspace.GameValues.Round.Value += 1
	if wave < 5 then
		
		for count=1,wave do
			mob.Spawn("fein", 2, map)
			mob.Spawn("fastboi", 1, map)
		end
		
	elseif wave ==  fifthWaves[wave] then
		print("boss wave", wave)
		mob.Spawn("fein", 10 * wave, map)
		task.wait(2)
		mob.Spawn("megafein", 1 * wave, map)
		mob.Spawn("monster", 1 * wave, map)
	
	elseif wave > 5 then
		for count=1,wave do
			mob.Spawn("fein", 1, map)
			mob.Spawn("megafein", 1, map)
			mob.Spawn("fastboi", 1, map)
			mob.Spawn("monster", 1, map)
		end
		
	end
	repeat
		task.wait(5)
		
	until #workspace.MobRealm:GetChildren() == 0 or gameOver
	
	if gameOver then
		print("GameOver pig")
		break
	end
	
	print("WAVE HAS ENDED:", wave)
	task.wait(1)
end

The wave is just set at the top and isn’t an actual value in the game

okay, i think I know what the problem is, change elseif wave == fifthWaves[wave] then to elseif wave == 5

You’re checking to see if the wave number is equal to some entry in a table, instead you need to check if there is an entry in that table at the given position. Try the following instead:

elseif fifthWaves[wave] ~= nil then

Alternatively, if you always want a boss fight every 5 waves, you can just use a modulus operation; see the following:

elseif wave % 5 == 0 then

Hope this helps. :slight_smile: