Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

How to save leaderboard points?

Asked by 14 days ago
Edited 14 days ago

I've tried looking online, youtube tutorials, etc. but none of the ones I found worked. Here's one of the codes that I made, similar to the online ones.
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("BucksStats")

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

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

local Bucks = Instance.new("StringValue")
Bucks.Name  = "Bucks"
Bucks.Parent = leaderstats
Bucks.Value = DataStore:GetAsync(player.UserId) or 0

end)

game.Players.PlayerRemoving:Connect(function(player) DataStore:SetAsync(player.UserId, player.leaderstats.Bucks.Value) end)

I used a regular (not module or local script) in ServerScriptService. Tested it, did the command :change OmegaNuker Cash 2, then rejoined the game, but I then had 0 Cash. I was expecting it to be 2 cash, but it didn't work that way.

0
It works when i tried it TTChaos 200 — 14d
0
Make sure that the game has dataStore turned on and it is published, otherwise it won't work, also i think it should be public but not sure.. Also if you changed the value on the client and not on the server it won't work too because it is only on the client and data store saves on the server. imKirda 731 — 14d
0
Basically he's saying, publish the game, go to settings and make sure you enable API services such as Datastores. Dovydas1118 795 — 14d
0
Thanks, that helped and it works now. OmegaNuker 0 — 14d

1 answer

Log in to vote
1
Answered by 14 days ago

I see critical flaws with your data. First of all, you're not using pcall. pcall is designed to catch errors and is very useful for situations such as Datastores. Also, it is recommended (by roblox) that you use game:BindToClose(), game:BindToClose() allows you to run code. After 30 seconds, it will shut down, regardless of the code. (but it should be fast enough to save the player's data.)

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("BucksStats")
game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats
    leaderstats.Parent = player

    local bucks = Instance.new("IntValue") --If you're using numbers, it is recommend that you use an IntValue instead.
    bucks.Name = "Bucks"
    bucks.Parent = leaderstats --You need to parent it to leaderstats, not the player.

    local data --You should add it in order to get the data. default variables are always equal to nil.
    local success, errorMessage = pcall(function() --If you did not add pcall, it would break your script.
        data = dataStore:GetAsync(player.UserId) or 0
    end)
    if success then --Alternative: If success == true
        print("Data found!") --You should add prints in order to troubleshoot.
        bucks.Value = data
    else
        warn("Error while getting data: "..errorMessage) --This gives some sort of code if there is any problem.
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, errorMessage = pcall(function()
        dataStore:SetAsync(player.UserId, player.leaderstats.Bucks.Value)
    end)
    if success then
        print("Data saved and player left") --You should add some sort of different message, in order to not confuse it between others.
    else
        warn("Error while saving data: "..errorMessage)
    end
end)

game:BindToClose(function()
    for _, player in pairs(game.Players:GetPlayers()) do --This gets the player in the server.
        local success, errorMessage = pcall(function()
            dataStore:SetAsync(player.UserId,player.leaderstats.Bucks.Value)
        end)
        if success then
            print("Data saved and game closed")
        else
            warn("Error while saving data: "..errorMessage)
        end
    end
end)
0
How did you learn how to script so well? iivSnooxy 216 — 14d
0
And what does GetAsync mean? iivSnooxy 216 — 14d
0
And what does GetAsync mean? iivSnooxy 216 — 14d
0
Devking. GetAsync means Get Asynchronous. It just gets the data. Dovydas1118 795 — 14d
Ad

Answer this question