Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this script give 1 level whenever the exp reaches 100?

Asked by 8 years ago

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)

3 answers

Log in to vote
0
Answered by 8 years ago
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)


0
Hope this helps thehybrid576 294 — 8y
0
Doesn't work, hybrid :C AltLaserthrasher1 13 — 8y
0
This won't support instances where the user's experience is more than twice the cap, and the user should be gaining 2 levels instead of 1. This also won't carry over the remaining experience if it's not divisible by the cap. ScriptGuider 5640 — 8y
0
good 4 u thehybrid576 294 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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.

0
Where exactly do I put it in the script, ScriptGuider? AltLaserthrasher1 13 — 8y
0
This isn't your script, this is the algorithm you can implement to solve the question you asked. Define the function I wrote in this answer, somewhere in your script, and call it whenever you need to get the levels from the user's experience value. ScriptGuider 5640 — 8y
Log in to vote
-1
Answered by 8 years ago

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.

0
Doesn't work, shabbs. AltLaserthrasher1 13 — 8y
0
idk then shabbs15 67 — 8y

Answer this question