Levelling up not working properly!

local LevelModule = require(game.ReplicatedStorage.LevelModule)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local tempData = ReplicatedStorage.tempData
local remotes = ReplicatedStorage.remotes
local bindable = ReplicatedStorage.bindable

local employeeCost = require(tempData.employeeCost)
local upgrades = game.ReplicatedStorage.upgrades
local updateOwned = remotes.updateOwned
local updatePrice = remotes.updatePrice
local coinStats = remotes.updateCoinStats
local retrieveOwned = bindable.retrieveOwned
local purchased = bindable.purchased
local upgradeActivated = remotes.upgradeActivated
local SaveEXP = bindable.SaveEXP

--This script handles everything related to employee shop.
--It handles the purchasing and the updating of the owned number.

upgradeActivated.OnServerInvoke = function(player, Name)
	--print("Something is being purchase!")
	local ownedprev = retrieveOwned:Invoke(player, Name) -- retrieve the number owned on the server side.
	local baseCost = employeeCost.defaultEmployee[tostring(Name)]
	local leaderstats = player:FindFirstChild("leaderstats")
	local coins = leaderstats.Coins
	if(coins.Value >= LevelModule.newCost(baseCost, ownedprev)) then
		local amount = LevelModule.newCost(baseCost, ownedprev)
		local success = purchased:Invoke(player, amount, Name)
		if success then
			local ownedafter = retrieveOwned:Invoke(player, Name)
			if ownedafter == 1 then
				print("EXP is being given for new employees.")
				local exp_gained = LevelModule.exp_earned(amount, 20)
				SaveEXP:Fire(player, exp_gained) -- increase EXP value of user
				print("SaveEXP fired!!")
				-- we also need to check if we need to level up!
				local getEXP = bindable.returnEXP:Invoke(player)
				print(getEXP)
				if(LevelModule.levelCheck(getEXP, leaderstats.Level.Value)) then 
					--print("Level check occurring.")
					--check if levelling up should occur.
					local EXP_Thresh = 25 * math.pow(leaderstats.Level.Value, 2)
					bindable.levelUp:Fire(player, EXP_Thresh) -- levels up user
				end
				
			else
				print("EXP is being given for old employees.")
				local exp_gained = LevelModule.exp_earned(amount, 50)
				SaveEXP:Fire(player, exp_gained) -- increase EXP value of user
				
				print("SaveEXP fired!!")
				--print(leaderstats.EXP.Value)
				local getEXP = bindable.returnEXP:Invoke(player)
				print(getEXP)
				if(LevelModule.levelCheck(getEXP, leaderstats.Level.Value)) then 
					--check if levelling up should occur.
					--print("Level check occurring.")
					local EXP_Thresh = 25 * math.pow(leaderstats.Level.Value, 2)
					bindable.levelUp:Fire(player, EXP_Thresh) -- levels up user
				end
			end
			
			updateOwned:FireClient(player)
			updatePrice:FireClient(player,Name) -- updates the price of item.
			coinStats:FireClient(player) --fire update coins to update coin stats.
			print(ownedafter.. Name.. " hired.")
			return nil
		else
			return "Purchase not successful."
		end
	else
		print("Not enough coins.")
		return "You need more coins."
	end
end

So I am creating a clicker game where the user will earn a lot of EXP when they initially purchase an upgrade and less EXP when they purchase an EXP of user. This part is working. I also wanted to check to make sure if user gains enough EXP, they level up! This part is not working. I was debugging the code and realize the program was looking at the previous EXP value. For example, if I had 25 EXP now, the code would say that I only have 18 EXP ( the number prior to gaining more EXP). Anyone know why this is happening? This is preventing me from levelling up the user on time!

This might be happening because the value gets updated very soon after the person clicks the thing the player needs to click to get EXP is this the entire script if not please show the entire script

1 Like

I edited the original post to include the whole script!!