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

How come leaderboard stats won't save?

local DataStoreService = game:GetService("DataStoreService")
local leaderstats = DataStoreService:GetDataStore("TEST")
local settings={
    groupid = 0
}

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


game.Players.PlayerAdded:connect(function(player)
     local leaderstats = Instance.new("IntValue",player)
     leaderstats.Name = "leaderstats"
 
     local rank = Instance.new("StringValue", leaderstats)
     rank.Name = "Rank"
     rank.Value = player:GetRoleInGroup(settings["groupid"])

    local points = Instance.new("IntValue", leaderstats)
    points.Name = "TEST"
    points.Value = points:GetAsync(key)
    points.Changed:connect(function() points:SetAsync(key, points.Value) end)
end)

1 answer

Try using this script (Yes I did make it myself.):

local data = game:GetService("DataStoreService"):GetDataStore("TEST")
local settings={
    groupid = 0
}

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.Value = 0
    points.Name = "TEST"
	if GetPlayerSave then
		points.Value = GetPlayerSave[1] or 0
	end
end)

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

5 comments

0

It still doesn't save when I leave and rejoin a game! Sorry, didn't work.

0

Oh silly me, I made a mistake. I have edited the script and fixed it. Sorry!

0

It uh... still doesn't work..

0

I edited ONCE more.

0

Yea it's not working, I don't know what's up.

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