DatastoreService: How can I SetAsync with Variable

My Question

Okay so I have a piece of code that kinda breaks the game. I have a remote function that is fired from the client and the server sends in a value or a string. So I have a question here.

So How can I use the :SetAsync function for a datastore using a variable. Like this situation

My Script

local settedAsync = data["Purchases"][ItemType][itemName]
settedAsync = "Equipable"
datastore:SetAsync(key, settedAsync)

If you can provide a way, please answer down below here.
( and yes a lot of people are telling me to use profile service, but i am trying regular datastores )

Images

And here is what the result is when I do that local variable.

What it is supposed to look like. But this is a example.

1 Like

I don’t know if I am supposed to use :UpdateAsync as I don’t know how to work it.

if anyone down here can explain the :UpdateAsync function a bit better than Roblox’s documentation then it will help me a lot! Please and thank you!

1 Like

:UpdateAsync() takes two parameters: The Key, and a function to set the data - both are required

The Key (you probably know what it is if you’ve used datastore before)
A callback function - takes in two arguments: The Current Value and DataStoreKeyInfo

Only the current value is needed
To set the value, just return a value

Example:

local dss = game:GetService("DataStoreService")

function addToCash(amount: number, player: Player)
   dss:UpdateAsync(player.UserId.."_Cash", function(currentvalue)
      player:WaitForChild("Cash").Value += amount
      return currentvalue + amount
   end)
end

game.Players.PlayerAdded:Connect(function(player)
   addToCash(10, player)
end)

This script adds 10 cash to the player’s cash value on join.

:UpdateAsync() is normally used when it is unnecessary to :GetAsync() and :SetAsync()