Kampfkarren / Roblox

Scripts and stuff I wrote for Roblox. Documentation is little to none as these are just stuff I took from my game that I thought I could share.
https://kampfkarren.github.io/Roblox/
Other
285 stars 78 forks source link

Datastore2 doesn't save when servers are shutdown. #114

Closed lorddebased closed 4 years ago

lorddebased commented 4 years ago

Only seems to save when players leave the game normally. Data just doesn't save on shutdown.

(I'm not the best when it comes to data issues so please bear with me)

The code below clones a folder from ServerStorage into the player, which contains all the player's data.

local PlayersService=game:GetService("Players")
local RS=game:GetService("ReplicatedStorage")
local SS=game:GetService("ServerStorage")

local DataStore2=require(1936396537)

local PlayersData={}

local function CreateData(Player)--Creates the player's data

    local function ConstructDataDictionnary()--Creates a table containing the values of the Data folder in ServerStorage
        local DefaultValue={}

        local DataFolder=SS:WaitForChild("Data")
        local DataDescendants=DataFolder:GetDescendants()

        for i=1,#DataDescendants do
            local CurrentValue=DataDescendants[i]
            if CurrentValue:IsA("ValueBase") then
                DefaultValue[CurrentValue.Name]={}
                local SubValue=DefaultValue[CurrentValue.Name]
                SubValue.Value=CurrentValue.Value
                pcall(function()
                    SubValue.MaxValue=CurrentValue.MaxValue
                    SubValue.MinValue=CurrentValue.MinValue
                end)
            end
        end

        return DefaultValue
    end

    local function GetPlayerData()--Calls the DataStore2 to get the player's data
        local DefaultValue=ConstructDataDictionnary()

        local PlayerDataStore2=DataStore2("Data2",Player)
        PlayerDataStore2:SetBackup(10)
        local PlayerData=PlayerDataStore2:GetTable(DefaultValue)

        return PlayerData,PlayerDataStore2
    end

    return GetPlayerData()
end

local function LoadData(Player,Data)

    local function ApplyDataToFolderSub(SubFolder)
        local SubFolderChildren=SubFolder:GetChildren()
        for i,v in pairs(SubFolderChildren) do

            --print(v.Name,Data[v.Name].Value)

            v.Value=Data[v.Name].Value
            pcall(function()
                v.MaxValue=Data[v.Name].MaxValue
                v.MinValue=Data[v.Name].MinValue
            end)
        end

    end

    local function ApplyDataToFolder(Folder)
        for i,v in pairs(Folder) do
            ApplyDataToFolderSub(v,Data)
        end

        return Folder
    end

    local function MakeDataFolder()--Clones the Data Folder into the Player
        local DataFolder={}
        for i,v in pairs(SS.Data:GetChildren()) do
            local Folder=v:Clone()
            Folder.Parent=Player
            DataFolder[i]=Folder
        end

        return DataFolder
    end

    local function GetCreateData()
        local DataFolder=MakeDataFolder()
        DataFolder=ApplyDataToFolder(DataFolder)

        return DataFolder
    end

    return GetCreateData()
end

local function ManageValues(DataFolder,PlayerData,PlayerDS2)
    local AllValues={}

    for _,v in pairs(DataFolder) do
        for i,vv in pairs(v:GetChildren()) do
            table.insert(AllValues,vv)
        end
    end

    for i=1,#AllValues do
        local Value=AllValues[i]
        --print(Value:GetFullName())

        Value.Changed:Connect(function(NewValue)

            local ValueChanged=PlayerData[Value.Name]
            if ValueChanged then
                ValueChanged.Value=NewValue
                PlayerDS2:Set(PlayerData)
            end

        end)

    end

end

local function PlayerAdded(Player)
    local PlayerData,PlayerDS2=CreateData(Player)
    local PlayerDataFolder=LoadData(Player,PlayerData)
    ManageValues(PlayerDataFolder,PlayerData,PlayerDS2)
    PlayersData[Player.Name]={PlayerData,PlayerDS2}
end

local function PlayerRemoving(Player)
    wait(5)
    PlayersData[Player.Name]=nil
end

PlayersService.PlayerAdded:Connect(PlayerAdded)
PlayersService.PlayerRemoving:Connect(PlayerRemoving)

for _,player in pairs (game.Players:GetPlayers()) do
    PlayerAdded(player)
end
Kampfkarren commented 4 years ago

Are you on the latest version? This is definitely not an issue anymore.

lorddebased commented 4 years ago

Are you on the latest version? This is definitely not an issue anymore.

Thank you for that, replaced the free model version with the latest version on the DataStore2 News/Changelog and everything works fine.

Thanks again. :)