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

I have made a leaderboard script but my level wont show up?

Asked by 5 years ago
Edited 5 years ago

I have made a Leaderboard Script which does not give any errors but everyone is supposed to have Level 1 to start with and whenever I test the game it says Level 0 for me? Why is that? It used to work but now it does not.. Please help me!

local DataStore = game:GetService("DataStoreService")
local lvl = DataStore:GetDataStore("LevelSaveSystem1")
local levels, exp = {}, {}

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
    levels[player] = Level
    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("LevelSaveSystem")

    local XP = Instance.new("IntValue",leaderstats)
    XP.Name = "XP"
    XP.Value = xp:GetAsync(player.UserId) or 0
    exp[player] = XP
    xp:SetAsync(player.UserId, XP.Value)--REMOVE THIS LINE IT MIGHT BE MESSING THE LEADERBOARD UP!
    XP.Changed:connect(function()XPChange(player,XP,Level) end)
        xp:SetAsync(player.UserId, XP.Value)
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, levels[player].Value)
    lvl:SetAsync(player.UserId, exp[player].Value)
    DataStore:GetDataStore("LevelSaveSystem1"):SetAsync(player.UserId, exp[player].Value)
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Server Script in ServerScriptService

local DataStore = game:GetService("DataStoreService")
local stats = 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 = 1

    local XP = Instance.new("IntValue", leaderstats)
    XP.Name = "XP"
    XP.Value = 0

    local LoadArray = stats:GetAsync(player.UserId)
    if LoadArray then
        player.leaderstats.Level.Value = LoadArray[1]
        player.leaderstats.XP.Value = LoadArray[2]
    end

    if player.leaderstats.Level.Value > 100 then
        player.leaderstats.Level.Value = 100
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local SaveArray = {player.leaderstats.Level.Value, player.leaderstats.XP.Value}
    stats:SetAsync(player.UserId, SaveArray)
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, XP, Level)
    if XP >= Level * 50 then
        player.leaderstats.XP.Value = 0
        player.leaderstats.Level.Value = Level + 1
    end
end)

So your GetAsyncs are a bit messy, so I reworked the script so the two values (Level and XP) are both saved to a single table (this way you only need one DataStore)

The other problem with that from before is that you called the changed events inside the player added event (but they were not localized), the above method circumvents this.

Since the Datastore calls the DataStore's Value first in your scenario, without checking if its nil, the level will stay at the defualt intvalue of 0, which is why I set the value when defining it, as well as the XP and Level values are different from the outset.

Your function pertaining to "XP Changed" looks like an incomplete remote event to me, which means that you would be giving values to the function, but they would have no corresponding property (.Value). If not, you can rewrite it as before. (In this example, I had a Remote Event named "Remote Event" within Replicated Storage)

0
Wow thank you! It gives me this error "RemoteEvent is not a valid member of ReplicatedStorage" diablotoken 6 — 5y
0
Make sure you have a remote event in replicated storage if you are using one for the function "XPChanged" SerpentineKing 3885 — 5y
Ad

Answer this question