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

Why doesn't it save the data in this dataStore script?

Asked by 4 years ago

I'm making a data store script and I can't seem to see an error. There is no error in the output either. Here is my script, pls fix if possible.

local DSS = game:GetService("DataStoreService")
local cashDSS = DSS:GetDataStore("CurrencyStore")

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

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

    local Currency = Instance.new("IntValue", leaderstats)
    Currency.Name = "Bucks"

    local data

    local success, errormsg = pcall(function()
        data = cashDSS:GetAsync(plr.UserId.."-Bucks")
    end)

    if success then
        Currency.Value = data
        print("data of "..plr.Name.." has been loaded") --//Prints this
    end

    if errormsg then
        print("Data of "..plr.Name.." couldn't be loaded") --//I'm assuming this works
        warn(errormsg)
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormsg = pcall(function()
        cashDSS:SetAsync(plr.UserId.."-Bucks", plr.leaderstats.Bucks.Value)
    end)

    if success then
        print("Successfully saved data of "..plr.Name) --//Doesn't print
    else
        print("Data could not be saved of "..plr.Name) --//Doesn't print
        warn(errormsg) --//Doesn't warn
    end
end)

I appreciate your time and effort. Thanks for helping.

1 answer

Log in to vote
1
Answered by
birds3345 177
4 years ago
Edited 4 years ago

This happened to me when I was making a datastore, but I found a fix. There is a roblox function called game:BindToClose, this event fires when all the players leave and keeps the server open until what is inside the function is done running. I suggest you make a function like SaveData so that way you don't have to put 2 duplicates of your code in 2 places. I really hope this helped.

0
Thanks I'll try it out now kingblaze_1000 359 — 4y
Ad

Answer this question