Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.