Selling DataStore2 value for another value

Currently trying to get my head around DataStore2, I see its advantages but have sadly already hit a wall haha

Im trying to sell my power for coins, then set my power to 0 as that value as been sold off, but I cant seem to get it to work.

local function onSellPower(player)
	local powerStore = DataStore2 ("power", player)
	local coinStore = DataStore2 ("coins", player)
	
	if powerStore:Get() >= 1 then
		print("hi")
		coinStore:Increment(powerStore:Get())
		print("hellow")
		wait(10)
		powerStore:Set(0)
	end
end

SellPower.OnServerEvent:Connect(onSellPower)

try not to update datastore2 too much as player can keep selling again and again which could cause issue just let the variable unsave but when the player leave the game you can save the data like this

 Players.PlayerRemoving:Connect(function(player)
    local powerStore = DataStore2 ("power", player)
    local coinStore = DataStore2 ("coins", player)
  -----set your data store 
 end)

You might be thinking of regular DataStore.

looking at the API you should never use PlayerRemoving with DataStore2.

Im trying to build a shop that you sell Power to and receive coins from

1 Like