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

SetAsync doesnt work, dont know whats wrong with it, no errors in script analysis or output window?

Asked by 4 years ago
Edited 4 years ago

Hello im trying to make a data store system but for some reason it doesnt save. There are no errors in the output or script analysis, and the few print lines i set up to see where the code stops, the last print i see in the output window is "This part loaded". I dont know why it doesnt work and i got no idea on how to fix it, heres my code:

local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("myDataStore")

print("Succesfully loaded dataStoreService and myDataStore")

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

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

    local Cookies = Instance.new("IntValue", leaderstats)
    Cookies.Name = "Cookies"
    Cookies.Value = 0
    Cookies.Parent = leaderstats

    playerUserId = "Player_"..player.UserId

    print("Leaderstats made")
    -- load data

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(playerUserId)
    end)    

    if success then
        -- set data 
        Cookies.Value = data
    end
    print("This part loaded")
end)

game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId
    local data = player.leaderstats.Cookies.Value
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)
    print("This thing happened")
    if success then
        print("Data successfully saved")
    else 
        print("Error happened")
        warn(errormessage)
    end
    print("Script ended")
end)

Something id also like to mention is that anything i want to print can print fine in the output window before i reach the myDataStore:SetAsync(playerUserId, data). Anything after it wont work. Thanks for help.

EDIT: Attempted to replace the PlayerRemoving with the following and i got an error saying "ServerScriptService.leaderstatshandler:51: attempt to index nil with 'UserId'", heres my code:

game:BindToClose(function(player)
    local playerUserId = "Player_"..player.UserId
    local data = player.leaderstats.Cookies.Value
    local success, errormessage = pcall(function()
        myDataStore:SetAsync(playerUserId, data)
    end)
    print("This thing happened")
    if success then
        print("Data successfully saved")
    else 
        print("Error happened")
        warn(errormessage)
    end
    print("Script ended")
end)
0
Same Problem Galaxybombboy 134 — 4y
0
Just a quick check, are API Services enabled? CreationNation1 459 — 4y
0
Yes, they are Phantom_User101 17 — 4y

1 answer

Log in to vote
1
Answered by
iOwn_You 543 Moderation Voter
4 years ago

When the last player in the server leaves PlayerRemoving doesnt fire. To save the data of the last player in the server you should use game:BindToClose() This will run the function before the server shuts down and save the data of the last player.

Keep in mind; BindToClose() will only keep the server from shutting down until the function finishes running or until 30 seconds have passed from the moment it started so make sure your function doesn't take that long to execute or you will have dataloss.

BindToClose API

0
Check the edited post, i got an error Phantom_User101 17 — 4y
0
BindToClose is not a substitute to PlayerRemoving, you need to use both. Also player is not a parameter for BindToClose(), you need to use GetPlayers and loop through the players and save their data. iOwn_You 543 — 4y
Ad

Answer this question