Make level system for tower defense game?

I’m making a tower defense game and I want a level system so I added 3 values to a players data: Level, Exp and RequiredExp. I then made this script:

	while wait() do
		if playerData.Exp >= playerData.RequiredExp then
			playerData.Exp = playerData.Exp - playerData.RequiredExp
			
			playerData.Level += 1
			playerData.RequiredExp = playerData.Level * 100

			
			levelupEvent:FireClient(player)
		end
	end
end

Then in a local script change the text to match the data. However, this only fires when they level up but I don’t wanna fire this like hundreds of times per second since I don’t think thats really good. Is there a more efficient way to do this?

3 Likes

this would make so the the ‘if’ statement runs every time exp value is changed (i might have made a mistake on the code but you get the basic idea of how it would work

3 Likes

But then it wouldn’t fire if they didn’t have enough to level up. Is just making two events efficient? One for leveling up and one for just updating the level?

3 Likes

Aren’t those the same thing? chars chars

2 Likes

Well I would do the checks on the server and if they leveled up do a level up event so it’s really just to avoid checking on the client

2 Likes

just do the .Changed Event on the client gui aswell so whenever any value of the three changes update all values on the gui

2 Likes

All you need for the data is the Exp, and then you can calculate the level and required exp based on that exp. Lets say each level was 100 Exp and I had 500 in my data, I would be Level 5 with 100 Exp required to the next level if that makes sense. Then all you have to do is whenever you grant a player xp on the server that’s when you’d check if the player exp is higher than the required amount.

local function GiveExp(player: Player, amount: number)
   local Data = -- Get PlayerData
   Data.Exp += amount

   if Data.Exp >= Required then
      print("Player has leveled up")
   end
end

--> Exp Event
GiveExp(player, 5) --> Gives 5 exp points to the player

The level table could look like this

{
 [1] = 100 --> Level 1: 100 Exp Required
 [2] = 200, --> Level 2: 200 Exp Required (100 Required if your level 1)
 [3] = 300, --> So on
}

Hopefully that makes sense, you can save the level if want but I prefer to save things that are needed. You can tweak all of this to your liking but hopefully that gives you a better understanding since doing it in a while loop is unnecessary

2 Likes

The thing is, the player gets exp once they join after doing a match in the game so I did this:

local function levelUp()
		if playerData.Exp >= playerData.RequiredExp then
			print("Level up")
			playerData.Exp = 0
			playerData.Level += 1
			playerData.RequiredExp = playerData.Level * 100
			print(playerData.Exp,playerData.Level,playerData)
			levelEvent:FireClient(player,"LevelUp",playerData.Exp,playerData.RequiredExp,playerData.Level)
		elseif playerData.Exp < playerData.RequiredExp then
			print("Change Exp!")
			levelEvent:FireClient(player,"ExpChange",playerData.Exp,playerData.RequiredExp,playerData.Level)
		end
	end
	levelUp()

But this only fires once due to you getting the exp only when you join back so I had to set the exp to 0 once they level up so its impossible to get multiple levels at once. Is there any way to make it so it repeats itself if there is still extra?

2 Likes

You should run it every time you need a player to receive exp. Due to how you currently have it setup, everything is fine though I’m not sure if you are giving the player exp unless that’s being handled elsewhere. I would make a function called GiveXP if you haven’t already.

2 Likes

The player actually recieves the exp to their data in the other game but for repeating if there’s extra, could I just do something like repeat playerData.Exp = playerData.Exp - playerData.RequiredExp until playerData.Exp < playerData.RequiredExp?

3 Likes