Fix percentage issue?

So I have this tower defense game and this gui shows the percentage done with the wave. I do this by having a value called MobsKilled and MobsNumber. But, if you skip a wave and theres still some mobs left, more mobs will come and if you kill enough, the mobs killed will be more than the number and you will get like 120%. Its not that big of an issue because it resets back to zero the next wave, its just kinda annoying. Any tips?

2 Likes

Could you provide any code for us to see what’s actually going on?

Sure. Heres the code to setting the text in a local script:

local function updatePercentage()
	gui.Info.Percentage.Text = 	math.round((workspace.Info.MobsKilled.Value/workspace.Info.MobNumber.Value)*100).."% Complete"
end

For every wave I do something like this

if wave == 1 then
		info.MobNumber.Value = 10

		mob.Spawn("Small Toilet",10,map)

Then in another script I increase the number of MobsKilled once one of them dies. Then at the start of the wave I reset both values back to 0 but the issue is, it can happen after the wave starts where the number of mobs on the map is more than what I set it to be because it was a previous wave.

Is the MobsKilled value reset to 0 after each wave?

Yeah I tried that but if you skip the wave when theres still some from the previous wave. It will reset back to zero but now there could be 50 mobs in total when theres only supposed to be 40 due to 10 being left from the previous wave.

consider looking into math.clamp

That would help to make it not go over 100% but I really just want the percentage gui to ignore enemies from the past wave and only focus on the current one.

Oh, I see your issue now. Yeah, you should probably look into either clearing all of the mobs before the next wave begins, or increasing the “MobsNumber” value to also include the mobs that are currently alive.

An example of these two would be:

  1. Clearing all mobs
-- Let's suppose there is a Folder in the Workspace which has all the mobs.
game.Workspace.Mobs:ClearAllChildren()
info.MobNumber.Value = 10
mob.Spawn("Small Toilet",10,map)
  1. Increasing MobsNumber
-- Let's suppose there is a Folder in the Workspace which has all the mobs.
info.MobNumber.Value = #game.Workspace.Mobs:GetChildren() + 10 -- getting the current amount of mobs, and adding 10
mob.Spawn("Small Toilet",10,map)
  1. Separating the mobs from the old wave and the new wave
    To accomplish this, you would have to set some sort of special attribute or place the mobs from the old wave into a separate location, and only detect the mobs in the new wave.

Yeah I was considering 2 and its probably the easiest method, I was just trying to see if there was any easy method to do 3. Thanks!

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