Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Cinorch 10
8 years ago
Edited 8 years ago
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)

0
The stats are saveing you just are using the wrong key line 14 and 26, "user" and "user_". User#5423 17 — 8y
0
So what should I replace them with instead? Cinorch 10 — 8y
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. User#5423 17 — 8y
0
So I'll just make the all the "user_" just "user" Cinorch 10 — 8y
View all comments (9 more)
0
It didn't work... Cinorch 10 — 8y
0
can you update the code pls. User#5423 17 — 8y
0
Updated, I just really want this to work.. Cinorch 10 — 8y
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). User#5423 17 — 8y
0
It would print 0, only nil and false are consider false for all all logical operators. User#5423 17 — 8y
0
So what you're saying I need to do is...? Cinorch 10 — 8y
0
swap them around, GetPlayerSave[1] or 0. you should also check that GetPlayerSave is not nil or this will error. User#5423 17 — 8y
0
Ah ok. Cinorch 10 — 8y
0
Could you possibly rewrite the script for me as an answer, please? So I understand better? Cinorch 10 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

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.

0
It comes out as "Value" instead of "Saves" on the leaderboard, is something wrong with it? Cinorch 10 — 8y
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). User#5423 17 — 8y
0
move line 22 to 16 ( before the loading of the stats). User#5423 17 — 8y
0
Okay, so it's name is correct, but the stats still won't save. Cinorch 10 — 8y
View all comments (3 more)
0
WAIT NEVERMIND IT FINALLY WORKS! Cinorch 10 — 8y
0
:D User#5423 17 — 8y
0
Thank you! Cinorch 10 — 8y
Ad

Answer this question