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

Why is my script failing to save data, but succesfully loading it?

Asked by 4 years ago

I've been recently trying to create a dataservice for saving the characters a player has to use, I do this by putting labeled boolvalues into a folder "CharactersUnlockedFolder" Though the script succesfully loads data, the script fails to save any data. There is unfortunately no errors or prints

local DSS = game:GetService("DataStoreService")
local CharacterDataStore = DSS:GetDataStore("CDS")
PlayersLeft = 0
local CharactersUnlockedFolder
game.Players.PlayerAdded:Connect(function(Player)
    PlayersLeft = PlayersLeft+1
    local Stats = Instance.new("Folder",Player)
    Stats.Name = "Stats"
    local Lives = Instance.new("IntValue",Stats)
    Lives.Name = "Lives"
    CharactersUnlockedFolder = Instance.new("Folder",Stats)
    CharactersUnlockedFolder.Name = "CharactersUnlocked"
    local Characters
    local success, errormessage = pcall(function()
        Characters = CharacterDataStore:GetAsync(Player.UserId.."-CharactersUnlocked")
    end)
    if success then
        print("Loaded data")
    else
        print("Error loading data")
        warn(errormessage)
    end
    if Characters ~= nil then
        for i,v in pairs(Characters) do
            print(v)
            local BoolValue = Instance.new("BoolValue",CharactersUnlockedFolder)
            BoolValue.Name = v.Name
        end
    end
end)
local BindableEvent = Instance.new("BindableEvent")
game.Players.PlayerRemoving:Connect(function(Player)
    PlayersLeft = PlayersLeft-1
    local success, errormessage = pcall(function()
        CharacterDataStore:SetAsync(Player.UserId.."-CharactersUnlocked",CharactersUnlockedFolder:GetChildren())
    end)
    if success then
        print("Saved data")
    else
        print("Error saving")
        warn(errormessage)
    end
    BindableEvent:Fire()
end)
game:BindToClose(function()
    while PlayersLeft > 0 do
        BindableEvent.Event:Wait()
    end
end)

Answer this question