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

Why does my level system not want to level up when I reach the needed XP amount?

Asked by 5 years ago
Edited 5 years ago

Okay so I tried making a level system, everything works apart from leveling up, I have tried to give myself "50" XP (That is how much is required to level up) my XP goes to 50 but my level does not seem to want to change to level 2.. I am not getting anything in the output box. Can someone help me please? I have been trying to fix this problem for so long now!

Here is the level up system code!

local DataStore = game:GetService("DataStoreService")
local lvl = DataStore:GetDataStore("LevelSaveSystem")

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"
    local Level = Instance.new("IntValue",leaderstats)
    Level.Name = "Level"
    Level.Value = lvl:GetAsync(player.UserId) or 1
    Level.Changed:connect(function()
        lvl:SetAsync(player.UserId, Level.Value)
    end)

    if (Level.Value > 100) then -- MaxLevel
        Level.Value = 100
    end

    local DataStore = game:GetService("DataStoreService")
    local xp = DataStore:GetDataStore("XPSaveSystem")

    local XP = Instance.new("IntValue",leaderstats)
    XP.Name = "XP"
    XP.Value = xp:GetAsync(player.UserId) or 0
    xp:SetAsync(player.UserId, XP.Value)
    XP.Changed:connect(function()
        xp:SetAsync(player.UserId, XP.Value)
    end)
        XP.Changed:connect(function()XPChange(player,XP,Level) end)

end)

function XPChange(player,XP,Level)
    if XP.Value >= Level.Value*50 then -- Takes 50 XP to get to Level 2 then 100 XP to get to Level 3 etc... also If you level up then...
        XP.Value = 0
        Level.Value = Level.Value+1
    end
end

game.Players.PlayerRemoving:connect(function(player)
    lvl:SetAsync(player.UserId, player.leaderstats.Level.Value)
    DataStore:GetDataStore("XPSaveSystem"):SetAsync(player.UserId, player.leaderstats.XP.Value)

end)
0
omg format your code please DeceptiveCaster 3761 — 5y
0
I did. diablotoken 6 — 5y
0
You have two functions connected to XP.Changed, try merging them into one. And use Connect (with capital "C"), as connect is deprecated. sleazel 1287 — 5y
0
Yeah I have done that, still does not work, but thank you for the help! diablotoken 6 — 5y
View all comments (4 more)
0
Would you like me to answer the script by rewriting it? Protogen_Dev 268 — 5y
0
Try doing a loop not a function aandmprogameing 52 — 5y
0
Thank you to everyone that has been willing to help me! I have finally fixed the script and it is now working! diablotoken 6 — 5y
0
0_0 HTHRWRU 6 — 5y

1 answer

Log in to vote
0
Answered by 4 years ago

I'm not sure, but have you tried giving yourself more than 50 XP?

Ad

Answer this question