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

DataStores not saving, how can I fix it?

Asked by 5 years ago

I've been working on a datastore script to save character customization data, but nothing's saving as it should, and no problems are displayed in studio, how can I fix it?

local Data_Store = game:GetService("DataStoreService")
local Character_Data = Data_Store:GetDataStore("Character")
local Players = game:GetService("Players")

local function Data_Values(Player)
    --Creating the values
    local Folder = Instance.new("Folder", Player)
    Folder.Name = (Player.Name.."Values")

    local SkinTone = Instance.new("IntValue", Folder)
    SkinTone.Name = "SkinTone"

    local HairOne = Instance.new("IntValue", Folder)
    HairOne.Name = "HairOne"

    local HairTwo = Instance.new("IntValue", Folder)
    HairTwo.Name = "HairTwo"

    local HairColour = Instance.new("IntValue", Folder)
    HairColour.Name = "HairColour"

    local Shirt = Instance.new("IntValue", Folder)
    Shirt.Name = "Shirt"

    local Pants = Instance.new("IntValue", Folder)
    Pants.Name = "Pants"

    local New = Instance.new("BoolValue", Folder)
    New.Name = "NewPlayer"
end

local function Get_Data(Player)
    local Key = (Player.Name.."Character")
    local Folder = Player[Player.Name.."Values"]

    local Data = Character_Data:GetAsync(Key)
    if Data then
        Folder.SkinTone.Value = Data[1]
        Folder.HairOne.Value = Data[2]
        Folder.HairTwo.Value = Data[3]
        Folder.HairColour.Value = Data[4]
        Folder.Shirt.Value = Data[5]
        Folder.Pants.Value = Data[6]
    else
        Folder.NewPlayer.Value = true
        Folder.SkinTone.Value = 1
        Folder.HairOne.Value = 1
        Folder.HairTwo.Value = 1
        Folder.HairColour.Value = 1
        Folder.Shirt.Value = 1
        Folder.Pants.Value = 1
        local SaveValues = {
        Folder.SkinTone.Value, Folder.HairOne.Value, Folder.HairTwo.Value,
        Folder.HairColour.Value, Folder.Shirt.Value, Folder.Pants.Value
        }
        local s, e pcall(function()
            Character_Data:SetAsync(Key, SaveValues)
        end)
        if s then
            print("New player's data has been saved.")
        else
            warn("Problem with saving new player's data")
        end
    end
end

local function Save_Data(Player)
    local Key = (Player.Name.."Character")
    local Folder = Player[Player.Name.."Values"]
    local Save_Table = {
        Folder.SkinTone.Value, Folder.HairOne.Value, Folder.HairTwo.Value,
        Folder.HairColour.Value, Folder.Shirt.Value, Folder.Pants.Value
    }
end


Players.PlayerAdded:Connect(function(Player)
    Data_Values(Player)
    Get_Data(Player)
end)

Players.PlayerRemoving:Connect(function(Player)
    Save_Data(Player)
end)

game:BindToClose(function()
    for _, v in pairs(Players:GetChildren()) do
        Save_Data(v)
    end
end)

Answer this question