DatastoreService: Question

Question

So look, here is a example on what I am kinda questioning for changing Values in a datastore. Can I use :SetAsync or do I use :GetAsync and then change the value?

local success, data = pcall(function()
	return datastore:GetAsync(player.UserId) -- Getting data
end)

data.Cash = 123 -- Would that work?

Or do use datastore:SetAsync(player.UserId)
I am kind of thinking that it is :SetAysnc. But I just want to ask the community for now.

( yes i know this is a small topic but i just want to know. )

2 Likes

SetAysnc takes a key, in most cases the player’s UserId, and sets it to the second parameter you pass into the function:

“SetAsync(key, value)”

GetAsync takes a key and returns a value:

“value = GetAsync(key)”

Hope this helps!

4 Likes

No. That wouldn’t work. :GetAsync() basically just returns a copy of the internal value you’re getting.

However, you can actually achieve what you’re trying to do using different methods. I won’t get too much into it, but you can load the player’s data when they join, put it into a table, and save that table when the player leaves. Now you can change that table while the player is ingame and it will save.

Popular datasaving resources like ProfileService use this method. It’s very efficient and I would recommend it.

1 Like

Thanks a lot. Really small answer, but was easy to know what you meant

2 Likes

I’ve been recommended to use ProfileService for a long time, as of right now I have made a big datastore script so I am not gonna restart, but the answer for me was really well. Thanks a lot!

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