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)

2 comments
Wow thank you! It gives me this error "RemoteEvent is not a valid member of ReplicatedStorage"
Make sure you have a remote event in replicated storage if you are using one for the function "XPChanged"