How to add my bool value to a dictionary?

I want to add a player’s changed/unsaved data to a dictionaryand send it to the server to save. For some reason when I print the table in the local script it doesn’t print anything or give any errors, but when it fires and tries to save it gives me an error saying I can’t save instances.

local toUpdate = {
}

if folder.HasPlayed.Changed then
	if debounce == false then
		print("value changed!")
		toUpdate["HasPlayed"] = folder.HasPlayed.Value
		task.wait()
		print(toUpdate)
	end
end

Datastores aren’t able to save Instances. You’ll need to serialize them (if you’re familiar with JavaScript and JSON it’s basically like JSON Objects)

You will need to serialize an object before saving it, and then “unserialize” it when retrieving your data.

If you need a more in-depth response, I can probably provide one

But it seems like you just have a BoolValue so I’d recommend just converting it into something like this: NameOfBool: true

I don’t think they’re intending to save an instance here, they are trying to save a dictionary that looks something like this {[“HasPlayed”] = true}. Something is happening in their server script that’s not saving the dictionary but is saving something else

2 Likes
  1. Does the script print “value changed!”?
  2. Could you please share the rest of your code to help diagnose the issue?

yes it prints “value changed!” but it doesn’t print the toUpdate under it.

here’s the server script:

function dataSave(player,toUpdate)
	local playerid = "Player" .. player.UserId
	local success, err = pcall(function()
		mystore:SetAsync(playerid,toUpdate)
	end)
	if success then
		print("data saved!" .. success .. toUpdate)
	else
		print(toUpdate)
	end
end

game.ReplicatedStorage.DataSave.OnServerInvoke = function(player, toUpdate)
	print(toUpdate)
	dataSave(player, toUpdate)
end

here it doesn’t print anything it only gives me the error that I can’t save an instance

I’ve tried to replicate the issue on my end, but the code runs perfectly fine. Leading me to believe the error lies somewhere else, potentially where you invoke the remote function.

  1. After “Value changed” has been outputted, are you not seeing a table similar to that in the image below?
    image
  2. Please provide the entirety of the script that fires the remote function

no :sob: that’s so weird

local player = game:GetService("Players").LocalPlayer
local folder = player:WaitForChild("PlayerDataFolder")
local gameslot = folder.CurrentGame.Value
local debounce = false

wait(3)

local toUpdate = {
}
local toUpdateBackup = {
	["TimePlayed"] = folder.TimePlayed.Value,
}

print(toUpdate)

function updateDataStore(forcefix)
if debounce == false then
	task.wait()
		debounce = true
		task.wait()
		game.ReplicatedStorage.DataSave:InvokeServer(player,toUpdate)
		print("sent!")
		task.wait()
end
end

if folder.CurrentGame.Changed then
		if folder.CurrentGame.Value >= 1 then
			task.wait()
			gameslot = folder.CurrentGame.Value
			task.wait()
			updateDataStore()
	end
end

if folder.HasPlayed.Changed then
	if debounce == false then
		print("value changed!")
		toUpdate["HasPlayed"] = folder.HasPlayed.Value
		task.wait()
		updateDataStore()
	end
end

Don’t pass the player through the remote function as it already sends the player by default.
You’re basically sending the arguments: (player, player, toUpdate).

2 Likes

Still learning how to use them :sweat_smile: thank you for the help!!

1 Like

No worres. One last thing, I believe you mean to write:

folder.HasPlayed.Changed:Connect(function()
	if debounce == false then
		print("value changed!")
		toUpdate["HasPlayed"] = folder.HasPlayed.Value
		task.wait()
		updateDataStore()
	end
end)

With the way you have it written right now, that block of code will only ever fire once. If you want it to fire every time the value is changed, use :Connect() as shown above

1 Like