So instead of using :SetAsync() I decided to instead use :UpdateAsync() however it returns the following error “Unable to cast value to function”
game.Players.PlayerRemoving:Connect( -- Saving Function
function(Player)
for Key, Value in pairs(DataSlots) do
Data[Value.Name] = Value.Value
end
DataBase:UpdateAsync(PlayerId, pcall(
function(OldData)
return Data
end)
)
end
)
Edit: I wasn’t sure what ‘Data’ was so I assumed you were returning a function and I thought that was your error. It’s actually a pcall error.
You can’t return ‘Data’ in the UpdateAsync function. It has to be the OldData parameter, which is the previous data the player had and you must return that.
For example, if you want to update a table:
--Table: {XP = 50}
ds:UpdateAsync(player.UserId, function(oldData)
oldData.XP = oldData.XP + 50
return oldData
end)
--Table after update: {XP = 100}
This is not correct. The old data parameter contains what is currently saved to a key. You do not need to return it explicitly; returning any value works and what you return will be processed to be set as the new key’s value.
OP’s issue is that they’re passing pcall instead of an actual function. Remove the pcall and it will work.
Is the issue that you’re using a Pcall() inside the UpdateAsync instead of an actual function?Consider wrapping your pcall around UpdateAsync instead as this should fix the issue.
In order to properly use pcalls with UpdateAsync, like the above mentioned, you will need to wrap the actual method UpdateAsync. Don’t return pcall’s results. Here’s a fixed copy of your code:
game.Players.PlayerRemoving:Connect( -- Saving Function
function(Player)
for Key, Value in pairs(DataSlots) do
Data[Value.Name] = Value.Value
end
pcall(DataBase.UpdateAsync, DataBase, PlayerId,
function(OldData)
return Data
end
)
end
)
I honestly prefer writing this a different way, but I’ve kept your code as-is and just changed the way you use pcall so it’s proper.
1 Like
Writing a different way to improve reliability? Or just changing the format in which I write the code? If it’s to improve my code I don’t mind hearing about it.
The way you write the code doesn’t really matter (as long as you use the proper syntax)