[SOLVED] Saving table, only saves key names not values

It only saves the names of the keys without their values ({HasPlayed} instead of {HasPlayed} = true

this is the module function for when a value changes

function playerModule:ValueChanged(player, value)
	print("module script")
	print(playerModule[player.Name])
	table.insert(playerModule[player.Name], value.Name)
	task.wait()
	playerModule[player.Name][value.Name] = value.Value
	print("added to table!")
	print(playerModule.toUpdate)
end

server script save function:

function dataSave(player)
	print(moduleScript[player.Name]) --- this prints {HasPlayed} = true, [1]HasPlayed
	local playerTable = moduleScript[player.Name]
	
	local success, err = pcall(function()
		mystore:SetAsync(player.UserId .. "_" .. key, playerTable)
	end)
	if success then
		print("data saved!")
		if not player then
			local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
			task.wait()
			table.clear(moduleScript[player.Name])
			print("table cleared!")
		else
			local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
			task.wait()
			table.clear(moduleScript[player.Name])
			print("table cleared!")
			local GetPlayerData = mystore:GetAsync(player.UserId .. "_" .. key)
			print(GetPlayerData) --- supposed to print same as first print, but instead prints {HasPlayed}
		end
	else
		print("data failed to save..." .. err)
	end
end
1 Like

Can you please show the code you are using to save?

1 Like

Yes so I actually fixed that, but now it only saves the names of the keys without their values ({HasPlayed} instead of {HasPlayed} = true

this is the module function for when a value changes

function playerModule:ValueChanged(player, value)
	print("module script")
	print(playerModule[player.Name])
	table.insert(playerModule[player.Name], value.Name)
	task.wait()
	playerModule[player.Name][value.Name] = value.Value
	print("added to table!")
	print(playerModule.toUpdate)
end

server script save function:

function dataSave(player)
	print(moduleScript[player.Name]) --- this prints {HasPlayed} = true, [1]HasPlayed
	local playerTable = moduleScript[player.Name]
	
	local success, err = pcall(function()
		mystore:SetAsync(player.UserId .. "_" .. key, playerTable)
	end)
	if success then
		print("data saved!")
		if not player then
			local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
			task.wait()
			table.clear(moduleScript[player.Name])
			print("table cleared!")
		else
			local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
			task.wait()
			table.clear(moduleScript[player.Name])
			print("table cleared!")
			local GetPlayerData = mystore:GetAsync(player.UserId .. "_" .. key)
			print(GetPlayerData) --- supposed to print same as first print, but instead prints {HasPlayed}
		end
	else
		print("data failed to save..." .. err)
	end
end
1 Like

Still haven’t been able to figure this out

In playerModule:ValueChanged, you use table.insert which is generally for arrays. When you use table.insert, it adds the value at the end of the array, but you are also setting playerModule[player.Name][value.Name] which is more appropriate for dictionary-like tables.

Here

function playerModule:ValueChanged(player, value)
    print("module script")
    print(playerModule[player.Name])
    playerModule[player.Name][value.Name] = value.Value
    print("added to table!")
    print(playerModule.toUpdate)
end
function dataSave(player)
    print(moduleScript[player.Name]) --- this prints {HasPlayed} = true, [1]HasPlayed
    local playerTable = moduleScript[player.Name]
    
    -- Print table contents to debug
    for k, v in pairs(playerTable) do
        print(k, v)
    end
    
    local success, err = pcall(function()
        mystore:SetAsync(player.UserId .. "_" .. key, playerTable)
    end)
    if success then
        print("data saved!")
        if not player then
            local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
            task.wait()
            table.clear(moduleScript[player.Name])
            print("table cleared!")
        else
            local updateModule = require(game.ReplicatedStorage:FindFirstChild("UpdateData"))
            task.wait()
            table.clear(moduleScript[player.Name])
            print("table cleared!")
            local GetPlayerData = mystore:GetAsync(player.UserId .. "_" .. key)
            -- Print fetched data to debug
            for k, v in pairs(GetPlayerData) do
                print(k, v)
            end
            print(GetPlayerData) --- supposed to print same as first print, but instead prints {HasPlayed}
        end
    else
        print("data failed to save..." .. err)
    end
end
2 Likes

okay i can’t tell whether i’m going insane or whether i am stupid and blind

but i don’t see key defined anywhere?

Thank you sm!

(nevermind, solved the new problem)

oh haha sorry i defined it at the top of the script so it’s not in my post

1 Like

mmm i’m not that experienced so i can’t help yo uwith your current issue

but i tried :pensive:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.