Wrong way of using UpdateAsync()?

Hello, im currently having an issue with a game im working on.

Before you say that i didn’t search around the devForums for a solution, as a lot of topics talk about UpdateAsync, What im looking for is different, All the other topics have to do with a different scenario.

Anyways, In a testing game, the code is supposed to save your “weapons” and “cash”, the code im using is just source code i found on a different topic, but thats not the point
Saving data for myself, such as leaving the game, or when the server closes (PlayerRemoving and BindToClose() are present in this script) Works fine for me, but for one of my friends, the function wont complete, when i test with my friend, only two of the first prints run, the last one doesnt, and his data isnt saved, in the output, whenever he leaves, i can see this.

What am i doing wrong?

local combineddata = {cash, weapons}

if combineddata then
	print("combined data exists")
	ds:UpdateAsync(plr.UserId, function(oldValue)
		print("started update async function")
		local previousData = oldValue or {DataId = 0}
		if combineddata.DataId == previousData.DataId then
			print("updating player data")
			combineddata.cash = cash
			combineddata.weapons = weapons
			return combineddata
		else
			return nil
		end
	end)
end
3 Likes

This may not be your issue, but make sure that all values being saved or loaded are not nil.

4 Likes

With more testing, it seems that data only saves for me (i am the first person to play the game that has this data saving method) So i removed the “If combineddata.dataId == previousData.dataid” part, and it works, but im unsure if its actually a good way now, is this code okay? or is it wrong?

if combineddata then
	print("data exists")
	ds:UpdateAsync(plr.UserId, function(oldValue)
		print("started update async")
		local previousData = oldValue or {DataId = 0}
		print("changin player stuff")
		if combineddata.cash and cash then
			combineddata.cash = cash
		end
		if combineddata.weapons and weapons then
			combineddata.weapons = weapons
		end

		return combineddata

	end)
end
1 Like

In this part, I’d use debug and open console when the friend is leaving the game. My fix for this though (and I’m an not good when it comes to programming datastores, but I’ll try), I’d add DataId += 1 underneath print("updating player data") and make it do SetAsync() if DataId is nil.

This way, data can be successfully created using SetAsync() if this is the first time the player joins the game and leaves. Any other will result in UpdateAsync().

Notes for critics

Yes, I am aware this is not efficient or whatever. I had this kind of problem until I came up with the idea to add SetAsync(). As long as you can teach me something new like your method by DMs or replies, then you can reply and please let me know.

1 Like

I completely forgot to add 1 to the DataId, My bad.

However, i tried this, and it returned an error, every other solution on the devForum is just the OP messing something up, but i dont understand why this happens with my script
script line:

combinedData.DataId = combinedData.DataId + 1

error:

attempt to perform arithmetic (add) on nil and number

Not exactly sure what i did wrong

1 Like

attempt to perform arithmetic (add) on nil and number is easy to read. You combined nil and a number together which is not going to go along.

I believe you had not check if the combinedData.DataId was nil before the process of saving.

1 Like

I know what the error means, i just dont know why it would be nil, as further up in the script, it checks stuff with the combinedData.Dataid, which doesnt error. Even when i added to make sure DataId wasnt nil, it would just never finish the function.

I’d use debugging and variable watch provided by Roblox at this point to figure out. You can DM me the recently modified section of the script and perhaps providing debugging (and variable watch) footage.

1 Like

I was just reading up on some other DataId-related posts here on the DevForum, Some people said that the DataId is never set, so it’ll always be nil, Was i supposed to set the DataId somewhere in the script? I thought it would do that automatically without me having to set it.

It depends on your use case. Like I said,

it would do SetAsync() everytime then, since DataId is always nil. i cant use SetAsync(), it overwrites all old data, which is why i started using UpdateAsync() in the first place

Not exactly. The first time the player joins/leaves, you set the DataId to 0 by SetAsync() if DataId is nil. The second or the countless times the player joins, the UpdateAsync() will take most of the job since DataId is not empty.

Use it kind of like setting up and updating software for analogy.

1 Like

I figured out what i was doing wrong, DataId was just an example thingy for the tutorial i used (whoops) Should i just remove the line where it compares past data’s DataId to the new data’s DataId? Or should i just add a DataId parameter to my “combineddata” table?

1 Like

It depends on your use for it. I’m glad you’ve solved the issue. I personally would keep it in case any players reports data issues (which is extremely unlikely but for backup).

1 Like

Thank you so much for your help, i will keep the code for the DataId. Just for confirmation, i add a Number at the start of the table called “DataId” just like adding data for anything else, right?

1 Like

Yep. That’s all for now. Feel free to rewrite your scripts as you experience. Don’t get too invested in my words though, got to use what you find convenient.

1 Like

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