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

Why doesnt my leaderboard save the stats?

Output:

16:42:03.239 - DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = 140702737 (x2)

Code:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(player)
	
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	
	local currency = Instance.new("IntValue",leader)
	currency.Name = "Coins"
	currency.Value = ds:GetAsync(player.UserId)
	ds:SetAsync(player.UserId, currency.Value)
	currency.Changed:connect(function()
		ds:SetAsync(player.UserId, currency.Value)
	end)
	
	local experience = Instance.new("IntValue",leader)
	experience.Name = "Exp"
	experience.Value = ds:GetAsync(player.UserId)
	ds:SetAsync(player.UserId, experience.Value)
	experience.Changed:connect(function()
		ds:SetAsync(player.UserId, experience.Value)
	end)
	
	local experiencelevel = Instance.new("IntValue",leader)
	experiencelevel.Name = "Level"
	experiencelevel.Value = ds:GetAsync(player.UserId)
	ds:SetAsync(player.UserId, experiencelevel.Value)
	experiencelevel.Changed:connect(function()
		ds:SetAsync(player.UserId, experiencelevel.Value)
	end)
end)

game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
	ds:SetAsync(player.UserId, player.leaderstats.Exp.Value)
	ds:SetAsync(player.UserId, player.leaderstats.Level.Value)
end)

1 answer

✓ Accepted answer

Hello! Your leaderboard should save stats, but, there is one problem:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(player)

    local leader = Instance.new("Folder",player)
    leader.Name = "leaderstats"

    local currency = Instance.new("IntValue",leader)
    currency.Name = "Coins"
    currency.Value = ds:GetAsync(player.UserId) or 0 -- I changed this so if there is a new player, their cash would not equal to 0.
    ds:SetAsync(player.UserId, currency.Value)
    currency.Changed:connect(function()
        ds:SetAsync(player.UserId, currency.Value)
    end)

    local experience = Instance.new("IntValue",leader)
    experience.Name = "Exp"
    experience.Value = ds:GetAsync(player.UserId) or 0 --Same here.
    ds:SetAsync(player.UserId, experience.Value)
    experience.Changed:connect(function()
        ds:SetAsync(player.UserId, experience.Value)
    end)

    local experiencelevel = Instance.new("IntValue",leader)
    experiencelevel.Name = "Level"
    experiencelevel.Value = ds:GetAsync(player.UserId) or 0 -- Also the same.
    ds:SetAsync(player.UserId, experiencelevel.Value)
    experiencelevel.Changed:connect(function()
        ds:SetAsync(player.UserId, experiencelevel.Value)
    end)
end)

game.Players.PlayerRemoving:connect(function(player)
    ds:SetAsync(player.UserId, player.leaderstats.Coins.Value)
    ds:SetAsync(player.UserId, player.leaderstats.Exp.Value)
    ds:SetAsync(player.UserId, player.leaderstats.Level.Value)
end)

Basically what I changed was that if there is a new player, their leaderstats would equal 0. That should work, also, check if API services are enabled. And, about that error, when you are testing in Studio, the data transfer is slower. So, that error is fine.

2 comments

0

And also, because you are saving it to the same datastore, it will save it incorrectly. So, I recommend using different datastores.

0

thanks, it works now

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