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

[SOLVED] Problem with saving bool value into Data Store?

Asked by
imKirda 4491 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Hello, i made a bool value saving system but it somehow does not works. Everything works well but it does not save the data when player leaves. With prints it prints "Trying to save data" but then nothing and nothing works.

local service = game:GetService("DataStoreService")
local dataStore = service:GetOrderedDataStore("BoolValueDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("IntValue",player)
    stats.Name = "BoolValues"
    local doorOpen = Instance.new("BoolValue", stats)
    doorOpen.Name = "DoorOpen"
    doorOpen.Value = false
    local data
    local success = pcall(function()
        data = dataStore:GetAsync(player.UserId,doorOpen.Value)
        end)
        if success then
            doorOpen.Value = data
            print("data restored")
        end
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        print("trying to save data")
    dataStore:SetAsync(player.UserId, player.BoolValues.DoorOpen.Value)
    print("data saved")
    end)
end)
0
code blocks pls kingblaze_1000 359 — 3y
0
Oh thanks for that tho i always thought it is automatical, imKirda 4491 — 3y
0
remove the extra argument of "GetAsync()" as it only requires the player's key. youtubemasterWOW 2741 — 3y
0
Thanks i have done that but still, line 23 does not work. imKirda 4491 — 3y
View all comments (2 more)
0
pcalls safely call functions so they don't stop the thread when they error which is why you have to do something like "local success, err = pcall(function() -- save data blahbahbhabha end) if not success then error(err) end" to get any results. y3_th 176 — 3y
0
Yes thanks a lot this is exactly what i needed or i mean by that i have found what the problem is in, you cannot save bool into ordered DataStore so instead of that you just have to use :GetDataStore() . Thanks you all for help i really appreciate it! imKirda 4491 — 3y

Answer this question