Don't close me! Ahh!
I've made a leaderboard script but can't for the life of me figure out how how to do this. I've been trying all day to make the exp reset once it reaches 100 and award 1 level.
Here's the leaderboard script:
local levelstore = game:GetService("DataStoreService"):GetDataStore("Levels") local expstore = game:GetService("DataStoreService"):GetDataStore("Exp") function saveInt(player, score, key) if player.userId > 0 then expstore:SetAsync(player.userId .. " " .. key, score ) end end function loadInt(player, stat, key) if player.userId > 0 then stat.Value = expstore:GetAsync(player.userId .. " " .. key) end end game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local level = Instance.new("IntValue", stats) level.Name = "Level" level.Value = 1 local exp = Instance.new("IntValue", stats) exp.Name = "Exp" exp.Value = 0 player:WaitForDataReady() loadInt(player, exp, "Exp") end) function playerRemoving(player) local stats = player:FindFirstChild("leaderstats") if stats ~= nil then saveInt(player, stats.Exp.Value, "Exp") end end game.Players.PlayerAdded:connect(function(player) game.Players.PlayerRemoving:connect(playerRemoving) end)
local levelstore = game:GetService("DataStoreService"):GetDataStore("Levels") local expstore = game:GetService("DataStoreService"):GetDataStore("Exp") function saveInt(player, score, key) if player.userId > 0 then expstore:SetAsync(player.userId .. " " .. key, score ) end end function loadInt(player, stat, key) if player.userId > 0 then stat.Value = expstore:GetAsync(player.userId .. " " .. key) end end game.Players.PlayerAdded:connect(function(player) local stats = Instance.new("IntValue", player) stats.Name = "leaderstats" local level = Instance.new("IntValue", stats) level.Name = "Level" level.Value = 1 local exp = Instance.new("IntValue", stats) exp.Name = "Exp" exp.Value = 0 player:WaitForDataReady() loadInt(player, exp, "Exp") --Do this exp.Changed:connect(function() if exp.Value >= 100 then level.Value = level.Value + 1 exp.Value = exp.Value - 100 --Save your values, not sure how you want it done end end) end) function playerRemoving(player) local stats = player:FindFirstChild("leaderstats") if stats ~= nil then saveInt(player, stats.Exp.Value, "Exp") end end game.Players.PlayerAdded:connect(function(player) game.Players.PlayerRemoving:connect(playerRemoving) end)
If I'm understanding you correctly, you want something that will increase a level by one each time your experience value is 100 or greater. For this, we can use a function called math.modf
to return how many levels, and how much remaining experience the user has. Here's a function that can implement this:
-- This function takes three arguments: -- * The experience the user has -- * The experience limit -- * The level of the user local function getLevelsFromExp(exp, expCap, currentLevel) local levels, remainExp = math.modf(exp / expCap) -- Get levels and remainder local leveledUp = currentLevel + levels -- Add level(s) to users level remainExp = math.floor(remainExp * expCap + 0.5) -- Get the remaining exp as a whole number (this will also round it) -- Return the users new level, and how much exp they have left. return leveledUp, remainExp end
Now we can simply test if this works by artificially creating a player with stats:
local user = {} user.expCap = 100 -- Experience required to level up user.exp = 120 -- How much experience the user has user.level = 1 -- Level of the user -- Call the function and return the data local newUserLevel, leftOverExp = getLevelsFromExp(user.exp, user.expCap, user.level) -- Display new user stats print("User is now level: " .. newUserLevel) print("User has: " .. leftOverExp .. " left") -- Save them to the user user.exp = leftOverExp user.level = newUserLevel
You might want to add something in there that also increases the expCap as the user levels up, but that's up to you. Hope this helped, let me know if you have any questions.
The answer is basic. If Statements.
if xp >= 100 then lvl = lvl + 1 xp = 0 end
The first line checks if 100 is equal to or greater than 100. The second line increases the level by 1. The third line resets the xp to 0.
Remember to switch out the variables. You might have to include a while true do loop so it checks every few seconds or you can make it so that in the script which increases the xp it checks then if the above is true.
I hope you find my answer useful and if so accept it.