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

Values not saving on Leaderboard?

local data = game:GetService("DataStoreService"):GetDataStore("Saves")
local data = game:GetService("DataStoreService"):GetDataStore("Gold")
local settings={
    groupid = 2903819
}

game:GetService("Players").PlayerAdded:connect(function(player)
    local key = "user_"..player.UserId  

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local rank = Instance.new("StringValue", leaderstats)
    rank.Name = "Rank"
    rank.Value = player:GetRoleInGroup(settings["groupid"])
    local GetPlayerSave = data:GetAsync("user_"..player.UserId) 
    local points = Instance.new("NumberValue", leaderstats)
    points.Name = "Saves"
    if GetPlayerSave  then
    points.Value = GetPlayerSave[1]  or 0
else
    points.Value  = 0
 end

local points = Instance.new("NumberValue", leaderstats)
    points.Name = "Gold"
    if GetPlayerSave  then
    points.Value = GetPlayerSave[1]  or 0
else
    points.Value  = 0
end

end)

game:GetService("Players").PlayerRemoving:connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local ToSave = {leaderstats.Saves.Value}
	local ToSave = {leaderstats.Gold.Value}
    data:SetAsync("user_"..player.UserId, ToSave)
end)

I want two things to save, Saves and Gold, both don't seem to work now despite it was working before when it had one value.

1 answer

I believe solving this would need folders to save each player's storage, otherwise returning the 2 values as nil.

P.S. Why do you need a 'Saves' value?

EDIT: Requested a fix by the owner

local data = game:GetService("DataStoreService"):GetDataStore("Saves")
local data = game:GetService("DataStoreService"):GetDataStore("Gold")
local settings={
    groupid = 2903819
}

game:GetService("Players").PlayerAdded:connect(function(player)
    local key = "user_"..player.UserId  

    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    local rank = Instance.new("StringValue", leaderstats)
    rank.Name = "Rank"
    rank.Value = player:GetRoleInGroup(settings["groupid"])
    local GetPlayerSave = data:GetAsync("user_"..player.UserId) 
    local gSaves = Instance.new("NumberValue", leaderstats) -- Replacing this part of the script.
    gSaves.Name = "Saves"
    if GetPlayerSave  then
    gSaves.Value = GetPlayerSave[1]  or 0
else
    gSaves.Value  = 0
 end

local points = Instance.new("NumberValue", leaderstats)
    points.Name = "Gold"
    if GetPlayerSave  then
    points.Value = GetPlayerSave[1]  or 0
else
    points.Value  = 0
end

end)

game:GetService("Players").PlayerRemoving:connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local ToSave = {leaderstats.Saves.Value}
    local ToSave = {leaderstats.Gold.Value}
    data:SetAsync("user_"..player.UserId, ToSave)
end)

5 comments

0

Just for example, also could you explain further by showing me via script of what I did wrong? It would help me a lot, thanks!

0

local points = Instance.new("NumberValue", leaderstats) was doubled, I believe, therefor it's been doubling.

0

Then how would I add a second value to the leaderboard stats in order for it to save?

0

Make a seperate variable for Line 16, instead of taking the name of Gold.

0

You think you could code it to show me? I really have a hard time understanding sometimes, I'm sorry!

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