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

SetAsync not actually saving when player leaves game?

Asked by 3 years ago
Edited 3 years ago

I am trying to make save and load, but I ran into one problem: SetAsync does not save, unless I set it manually. I have tried datastore plugins such as DataStoreX, but none have worked. Code:

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("DAT")

game.Players.PlayerAdded:Connect(function(player)
    local coins = Instance.new("IntValue")
    coins.Name = "coins"
    coins.Parent = player

    local playerUserId = "Player_"..player.UserId
    local success, err = pcall(function()
        coins.Value = dataStore:GetAsync(playerUserId)
    end)

    if success then
        print("Load success!")
    else
        warn(err)
    end
    print(player.coins.Value)
end)

game.Players.PlayerRemoving:Connect(function(player)
    local success, err = pcall(function()
        print(player.coins.Value)
        local playerUserId = "Player_"..player.UserId
        dataStore:SetAsync(playerUserId, player.coins.Value)
    end)

    if success then
        print("Save success!")
    else
        warn(err)
    end
end)

game:BindToClose(function()
    wait(5)
end)

NOTE #1: I TRIED SAVING IN BOTH BINDTOCLOSE AND PLAYERREMOVING, BUT IT STILL DIDN'T SAVE. NOTE #2: I'M RUNNING INSIDE ROBLOX STUDIO

0
Put stuff in the BindToClose that saves data for everyone in the server, should work then. greatneil80 2647 — 3y
0
^ yeah, you should save during PlayerRemoving and BindToClose OfficerBrah 494 — 3y
0
I tried that, except i get this error: DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = Player_1217140517 Bigmancozmo 7 — 3y
0
hello? Bigmancozmo 7 — 3y
0
that's fine as long as u don't spam save every second. greatneil80 2647 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

I tried it, and it didn't happen that it won't save. Try to restart the studio or restart the computer. Image: https://gyazo.com/4541af38180329af133b8f6e121eacbb

0
That didn't work. I have Datastore Editor, but can only change data with that. I also restarted my Macbook and Studio a few hours ago. Bigmancozmo 7 — 2y
0
btw heres what i mean: I have a button. I click the button 5 times. My number that is meant to be saved is 5. I leave the game. In the script, inside my PlayerRemoving, it is printing the value of my coins. It prints 0, instead of 5. I play it again. It also prints 0 to the console. Bigmancozmo 7 — 2y
Ad

Answer this question