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

DataStore working for most variables but not all?

Asked by 4 years ago
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")


game.Players.PlayerAdded:Connect(function(player)
    local Original
    local UFO
    local OOB
    local Bacon
    local success, errormessage = pcall(function()
        Original = myDataStore:GetAsync(player.UserId.."-OriginalCookie")
        UFO = myDataStore:GetAsync(player.UserId.."-UFOCookie")
        OOB = myDataStore:GetAsync(player.UserId.."-OOBCookie")
        Bacon = myDataStore:GetAsync(player.UserId.."-BaconCookie")
    end)
    if success then
        shared.OriginalCookie = Original
        shared.UFOCookie = UFO
        shared.OOBCookie = OOB
        shared.BaconCookie = Bacon
    else
        warn(errormessage)
        if Original == nil then
            shared.OriginalCookie = false
        end
        if UFO == nil then
            shared.UFOCookie = false
        end
        if OOB == nil then
            shared.OOBCookie = false
        end
        if Bacon == nil then
            shared.BaconCookie = false
        end
    end

end)




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

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-OriginalCookie",shared.OriginalCookie)
        myDataStore:SetAsync(player.UserId.."-UFOCookie",shared.UFOCookie)
        myDataStore:SetAsync(player.UserId.."-OOBCookie",shared.OOBCookie)
        myDataStore:SetAsync(player.UserId.."-BaconCookie",shared.BaconCookie)
    end)

    if success then
        print("Data saved!")
    else
        print("Data save error.")
        warn(errormessage)
    end

end)

If you look specifically at the OOB saves, for some reason it always prints false when I do print(shared.OOBCookie), and the only way it can come up false is if there is an error, because no script anywhere sets those shared variables to false, they are either true or nil. Although, I don't get a warning in the output like I'm supposed to, and everything else saves fine. I also know that when I end the playing that shared.OOBCookie is true. Can somebody help me?

Answer this question