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

The values in my code wont save in the data store?

Asked by 3 years ago

As in the title above, im not sure why my code isnt working, there are no errors but the values will not save at all.

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

local function onplayerjoin(player)
    local folder = Instance.new("Folder")
    folder.Name = "Locker"
    folder.Parent = player

    local ShirtA = Instance.new("BoolValue")
    ShirtA.Name = "LumberShirt"
    ShirtA.Parent = folder

    local PantsA = Instance.new("BoolValue")
    PantsA.Name = "NicePants"
    PantsA.Parent = folder


    local playeruserid = 'Player_'..player.UserId
    local data = playerData:GetAsync(playeruserid)
    if data then
        ShirtA.Value = data['LumberShirt']
        PantsA.Value = data['NicePants']
    else
        ShirtA.Value = false
        PantsA.Value = false
    end
end

local function create_table(player)
    local player_stats = {}
    for _, stat in pairs(player.Locker:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function onplayerexit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playeruserid = 'Player_'..player.UserId
        playerData:SetAsync(playeruserid, player_stats)
        print('Data saved')
    end)

    if not success then
        warn('Could not save data.')
    end
end

game.Players.PlayerAdded:Connect(onplayerjoin)
game.Players.PlayerRemoving:Connect(onplayerexit)

1 answer

Log in to vote
0
Answered by 3 years ago

the folder for leaderstats needs to be named: "leaderstats" like that, all caps sensitive.

like this:

local function onplayerjoin(player)
    local folder = Instance.new("Folder")
    folder.Name = "leaderstats"
    folder.Parent = player

    local ShirtA = Instance.new("BoolValue")
    ShirtA.Name = "LumberShirt"
    ShirtA.Parent = folder

    local PantsA = Instance.new("BoolValue")
    PantsA.Name = "NicePants"
    PantsA.Parent = folder


    local playeruserid = 'Player_'..player.UserId
    local data = playerData:GetAsync(playeruserid)
    if data then
        ShirtA.Value = data['LumberShirt']
        PantsA.Value = data['NicePants']
    else
        ShirtA.Value = false
        PantsA.Value = false
    end
end

local function create_table(player)
    local player_stats = {}
    for _, stat in pairs(player.leaderstats:GetChildren()) do
        player_stats[stat.Name] = stat.Value
    end
    return player_stats
end

local function onplayerexit(player)
    local player_stats = create_table(player)
    local success, err = pcall(function()
        local playeruserid = 'Player_'..player.UserId
        playerData:SetAsync(playeruserid, player_stats)
        print('Data saved')
    end)

    if not success then
        warn('Could not save data.')
    end
end

game.Players.PlayerAdded:Connect(onplayerjoin)
game.Players.PlayerRemoving:Connect(onplayerexit)
Ad

Answer this question