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
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
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.
After “Value changed” has been outputted, are you not seeing a table similar to that in the image below?
Please provide the entirety of the script that fires the remote function
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).
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