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

datastore not functioning, right after it loads it turns to zero?

Asked by 4 years ago

If you don't want to help, don't help, for those who want to help; try your best :), and if you can't dont leave an answer.

Problem

It's supposed to work like a normal datastore, but obviously things never work for me so there was something wrong; When my friend joined, i saw his old datastore go to his for a split second and then it went back to zero for a reason?

Script

local DataStoreService = game:GetService("DataStoreService")
local StatsData = DataStoreService:GetDataStore("Savers")

local key = "Player_"

game.Players.PlayerAdded:Connect(function(player)
    local sucess, errormessage = pcall(function()
        local retrievedData = StatsData:GetAsync(key .. player.UserId)
        local leaderstats = player.leaderstats

        leaderstats.Cash.Value = retrievedData[1]
        leaderstats.Power.Value = retrievedData[2]
        leaderstats.Rebirths.Value = retrievedData[3]
    end)
end)

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

    local leaderstats = player.leaderstats

    local sucess, errormessage = pcall(function()
        StatsData:SetAsync(key .. player.UserId, {leaderstats.Cash.Value, leaderstats.Power.Value, leaderstats.Rebirths.Value})
    end)

    if sucess then
        print("Was a succesfull data store!")
    else
        warn(errormessage)
    end
end)
0
Try using a loop; this article may help you: https://developer.roblox.com/en-us/articles/Saving-Player-Data JesseSong 3916 — 4y
1
Why does people downvote this. She did nothing boi Xapelize 2658 — 4y

1 answer

Log in to vote
3
Answered by 4 years ago
Edited 4 years ago

Datastores may fail to work sometimes, so you need to keep saving/loading data until it's successful.

local tries = 0 -- For debugging

if sucess then
    print("Success")
else
    while not sucess do
        print("Error: ", errormessage)
    sucess, errormessage = pcall( -- GetAsync/SetAsync function 
    )
    tries = tries + 1
    wait() -- Prevent a script timeout error
    end
end

print("Attempts to save/load: ", tries)
Ad

Answer this question