Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am having issues with my leaderboard! Please help me?

I have made a leaderboard script that has levels and xp, I wanted to add money but now my levels dont work as everyones level is supposed to be level 1 but once I added money to the leaderboard the level is 0 for everyone.. I also tried to Store the Data.. But that does not want to work as "cry" is a nil value on line 54, please help me with this! I really want this to work and have been trying to get it working for 2 days now!

I just want to make a simple leaderboard script that has Levels, XP, Money and can save them to the server..

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

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.Changed:connect(function()XPChange(player,XP,Level) end)
        xp:SetAsync(player.UserId, XP.Value)

    local DataStore = game:GetService("DataStoreService")
    local cry = DataStore:GetDataStore("MoneySaveSystem")

	local Coins = Instance.new("IntValue",leaderstats)
	Coins.Name = "Coins" -- Currency name here
	Coins.Value = cry:GetAsync(player.UserId) or 0 -- Starting money
    bills[player] = Coins
        cry:SetAsync(player.UserId, Coins.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)
    cry:SetAsync(player.UserId, bills[player].Value)
    DataStore:GetDataStore("MoneySaveSystem"):SetAsync(player.UserId, bills[player].Value)
end)

3 comments

0

Cry was declared as a local variable of Line 32, it cannot be referenced outside of it’s scope

0

I think we can all tell this Script isn’t yours either

0

It is not all mine but most parts of it are.. I have been working on this for 2 days now coming onto 3.

1 answer

✓ Accepted answer

Server Script in ServerScriptService

local DataStore = game:GetService("DataStoreService")
local stats = DataStore:GetDataStore("StatSaveSystem")

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 Coins = Instance.new("IntValue", leaderstats)
	Coins.Name = "Coins"
	Coins.Value = 0

	local LoadArray = stats:GetAsync(player.UserId)
	if LoadArray then
		player.leaderstats.Level.Value = LoadArray[1]
		player.leaderstats.XP.Value = LoadArray[2]
		player.leaderstats.Coins.Value = LoadArray[3]
	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, player.leaderstats.Coins.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)

As I stated Here: https://scriptinghelpers.org/questions/76529/i-have-made-a-leaderboard-script-but-my-level-wont-show-up

You only need to use one datastore if you use a single table. For the remote event at the end of the script, make sure to have a Remote Event in Replicated Storage (mine is named "RemoteEvent")

1 comment

0

Yes thank you so much! It all works now!

This discussion is preserved as it was. New answers, comments, and votes are closed.