How do I implement a levelup system?

I’m here again to ask for your help, I would like to implement a level up system in the script, but what I managed to do is just update when I enter.

Like, I would like a way in which when the exp changes and if it is greater than or equal to the necessary exp, it evolves you without having to leave the game.

Can someone help me?

My script is this:

local DataStoreService = game:GetService("DataStoreService")
local CornerData: DataStore = DataStoreService:GetDataStore("DBGameV10")

local player_default_data = {
	Class = "None",
	Bounty = 0,
	Levels = 1,
	Gold = 0,
	Gems = 0,
	Exp = 0,
	ExpNeed = 170,
	HealthAdd = 0,
	DefenseP = 0,
	SwordP = 0,
	LuckP = 0,
	SpecialP = 0,
	PointsS = 1,

	Defense = 0,
	Sword = 0,
	Luck = 0,
	Special = 0,
	Points = 3,
	PvpSystem = 0,
}

local function load(key)
	local received, result = pcall(function()
		local dataReceived = CornerData:GetAsync(key)
		return dataReceived
	end)

	if received then
		print('Loaded')
		return result or player_default_data  
	else
		warn(result) 
	end
end

local function save(key, data)

	local success, errorMessage = pcall(function()
		CornerData:SetAsync(key, data)
	end)

	if success then
		print('Saved')
	else
		warn(errorMessage)
	end

end

game.Players.PlayerAdded:Connect(function(player)
	local dataIsLoading = Instance.new('BoolValue', player) 
	dataIsLoading.Name = 'DataIsLoading'

	local playerData = load(player.UserId)  

	if playerData then
		player.CharacterAdded:Connect(function(char)
			char.Humanoid.MaxHealth = 100 + player:WaitForChild("Data").HealthAdd.Value
			char.Humanoid.Health = char.Humanoid.MaxHealth
		end)

		dataIsLoading:Destroy()  

		-- Folders
		local infoFolder = Instance.new('Folder', player)
		infoFolder.Name = "Data"

		local leaderFolder = Instance.new('Folder', player)
		leaderFolder.Name = "leaderstats"

		-- Databases
		local bounty = Instance.new('IntValue', leaderFolder)
		bounty.Name = 'Bounty'
		bounty.Value = playerData.Bounty

		local level = Instance.new('IntValue', infoFolder)
		level.Name = 'Levels'
		level.Value = playerData.Levels

		local exp = Instance.new('IntValue', infoFolder)
		exp.Name = 'Exp'
		exp.Value = playerData.Exp

		local expneed = Instance.new('IntValue', infoFolder)
		expneed.Name = 'ExpNeed'
		expneed.Value = playerData.ExpNeed

		local gold = Instance.new('IntValue', infoFolder)
		gold.Name = 'Gold'
		gold.Value = playerData.Gold

		local gems = Instance.new('IntValue', infoFolder)
		gems.Name = 'Gems'
		gems.Value = playerData.Gems

		local Beginnerclass = Instance.new('StringValue', infoFolder)
		Beginnerclass.Name = 'Class'
		Beginnerclass.Value = playerData.Class

		local healthadd = Instance.new('IntValue', infoFolder)
		healthadd.Name = 'HealthAdd'
		healthadd.Value = playerData.HealthAdd

		local defensep = Instance.new('IntValue', infoFolder)
		defensep.Name = 'DefenseP'
		defensep.Value = playerData.DefenseP

		local swordp = Instance.new('IntValue', infoFolder)
		swordp.Name = 'SwordP'
		swordp.Value = playerData.SwordP

		local luckp = Instance.new('IntValue', infoFolder)
		luckp.Name = 'LuckP'
		luckp.Value = playerData.LuckP

		local specialp = Instance.new('IntValue', infoFolder)
		specialp.Name = 'SpecialP'
		specialp.Value = playerData.SpecialP

		local pointss = Instance.new('IntValue', infoFolder)
		pointss.Name = 'PointsS'
		pointss.Value = playerData.PointsS

		-- Stats
		local defense = Instance.new('IntValue', infoFolder)
		defense.Name = 'Defense'
		defense.Value = playerData.Defense

		local sword = Instance.new('IntValue', infoFolder)
		sword.Name = 'Sword'
		sword.Value = playerData.Sword

		local luck = Instance.new('IntValue', infoFolder)
		luck.Name = 'Luck'
		luck.Value = playerData.Luck

		local special = Instance.new('IntValue', infoFolder)
		special.Name = 'Special'
		special.Value = playerData.Special

		local points = Instance.new('IntValue', infoFolder)
		points.Name = 'Points'
		points.Value = playerData.Points

		local pvpsystem = Instance.new('IntValue', infoFolder)
		pvpsystem.Name = 'PvpSystem'
		pvpsystem.Value = playerData.PvpSystem	
	else
		warn('DATA FAILED TO LOAD')
		player:Kick('Your data failed to load, please rejoin.') 
	end

end)

game.Players.PlayerRemoving:Connect(function(player)
	local infoFolder = player:FindFirstChild("Data")
	local leaderFolder = player:FindFirstChild("leaderstats")
	local playerDataLoaded = player:FindFirstChild('DataIsLoading') == nil  

	if infoFolder and playerDataLoaded then
		player_data = {
			Class = infoFolder.Class.Value,
			Bounty = leaderFolder.Bounty.Value,
			Levels = infoFolder.Levels.Value,
			Gold = infoFolder.Gold.Value,
			Gems = infoFolder.Gems.Value,
			Exp = infoFolder.Exp.Value,
			ExpNeed = infoFolder.ExpNeed.Value,
			HealthAdd = infoFolder.HealthAdd.Value,
			DefenseP = infoFolder.DefenseP.Value,
			SwordP = infoFolder.SwordP.Value,
			LuckP = infoFolder.LuckP.Value,
			SpecialP = infoFolder.SpecialP.Value,
			PointsS = infoFolder.PointsS.Value,
			Defense = infoFolder.Defense.Value,
			Sword = infoFolder.Sword.Value,
			Luck = infoFolder.Luck.Value,
			Special = infoFolder.Special.Value,
			Points = infoFolder.Points.Value,
			PvpSystem = 0,
		}

		save(player.UserId, player_data) 
	end
end)

game:BindToClose(function()
	for _,Player in pairs(game.Players:GetPlayers()) do
		task.wait(#game.Players:GetPlayers() > 1 and #game.Players:GetPlayers()/2 or 1)
		local infoFolder = Player:FindFirstChild("Data")
		local leaderFolder = Player:FindFirstChild("leaderstats")
		player_data = {
			Class = infoFolder.Class.Value,
			Bounty = leaderFolder.Bounty.Value,
			Levels = infoFolder.Levels.Value,
			Gold = infoFolder.Gold.Value,
			Gems = infoFolder.Gems.Value,
			Exp = infoFolder.Exp.Value,
			ExpNeed = infoFolder.ExpNeed.Value,
			HealthAdd = infoFolder.HealthAdd.Value,
			DefenseP = infoFolder.DefenseP.Value,
			SwordP = infoFolder.SwordP.Value,
			LuckP = infoFolder.LuckP.Value,
			SpecialP = infoFolder.SpecialP.Value,
			PointsS = infoFolder.PointsS.Value,
			Defense = infoFolder.Defense.Value,
			Sword = infoFolder.Sword.Value,
			Luck = infoFolder.Luck.Value,
			Special = infoFolder.Special.Value,
			Points = infoFolder.Points.Value,
			PvpSystem = 0,
		}
		local success, err  = pcall(function()
			save(Player.UserId,player_data) 
		end)
	end
end)

This is pretty tricky and there is no one perfect answer.
Try using the tutorials below and once you get the results from levelling the player up, load that onto the players save data so it persists.

Again you need to learn alot here so it’s not gonna be very easy.

1 Like

Create a Module or Class script that checks every time someone gains XP if the XP is enough for a level up. If the XP is enough, the script will update all the appropriate values.

1 Like