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

Someone please tell me what's wrong with this stats save script? Leaderboard stats won't save.

local data = game:GetService("DataStoreService"):GetDataStore("Saves")
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.Value = 0 or GetPlayerSave[1] 
    points.Name = "Saves"
end)

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

13 comments

0

The stats are saveing you just are using the wrong key line 14 and 26, "user" and "user_".

0

So what should I replace them with instead?

0

You should use the same key to save and load the data but you can use multiple keys just make shure the key links to the correct data saved.

0

So I'll just make the all the "user_" just "user"

0

It didn't work...

0

can you update the code pls.

0

Updated, I just really want this to work..

0

The problem is that you have changed the logic, 'The operator or returns its first argument if it is not false' e.g. if you test print(0 or 1).

0

It would print 0, only nil and false are consider false for all all logical operators.

0

So what you're saying I need to do is...?

0

swap them around, GetPlayerSave[1] or 0. you should also check that GetPlayerSave is not nil or this will error.

0

Ah ok.

0

Could you possibly rewrite the script for me as an answer, please? So I understand better?

1 answer

✓ Accepted answer

Data stores are very complicated to manage as there is a lot of things that can error, here is a link that can explain the limitations.

We save data to a key which we then can use to access this saved data, the key itself can be anything and in some cases it is used to store game settings with pre defined keys. The player user id is added to the key so that we can make all keys unique to that user.

There are two format that we can use to save data with a key, 1st we can save the raw data to the key e.g. a string, the second format which is the one you have used it to save the data in a table ( though you need to use {} and not (). The data store cannot save objects, to save these we would need to process then into a saveable format. This is known as serialisation and deserialization.

Back to your code:-

local data = game:GetService("DataStoreService"):GetDataStore("Saves")
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) -- we currently do not know if the player has data so 'GetPlayerSave ' can be nil
    local points = Instance.new("NumberValue", leaderstats)
    points.Value = 0 or GetPlayerSave[1] -- as mentioned before we need to swap this logic around
    points.Name = "Saves"
end)

game:GetService("Players").PlayerRemoving:connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local ToSave = (leaderstats.Saves.Value) -- we need to use a table here {} and not ()
    data:SetAsync("user_"..player.UserId, ToSave)
end)

Basic changes:-

local data = game:GetService("DataStoreService"):GetDataStore("Saves")
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)

    if GetPlayerSave  then
	points.Value = GetPlayerSave[1]  or 0 -- we may have data but no value so its best to check
else
	points.Value  = 0 -- no player data
end
    points.Name = "Saves"
end)

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

This includes the basics of what needed to change, this currently does not manage any other exceptions that may be thrown by the data store. Lastly there are change that can be made to simplify this code.

I hope this helps, please comment if you do not understand how / why this code works.

7 comments

0

It comes out as "Value" instead of "Saves" on the leaderboard, is something wrong with it?

0

I think I know what is it on line 22 we set the name, but it takes time to load in the user data meaning that the name change does not replicate ( thats what i got at least).

0

move line 22 to 16 ( before the loading of the stats).

0

Okay, so it's name is correct, but the stats still won't save.

0

WAIT NEVERMIND IT FINALLY WORKS!

0

:D

0

Thank you!

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