How would I check if the player has enough currency in datastore to buy an item?

I am currently working on a currency where players can buy one time products with it. However I have came to a problem, I am unsure about how to go with checking the player has enough cash and if they do then give them the item otherwise send a GUI to notify them.

Thanks!

2 Likes

Is the cash stored as a Number/IntValue, inside a table or what?

Its Pretty simple

local Cash = player.leaderstats.cash
if Cash.Value >= Cost then
cash.Value -= Cost
--give item
end
4 Likes

Yes, I was just making sure that I knew what the “cash” value is (Number/IntValue, stored inside a table, etc.). That is correct though.

1 Like

Inside a datastore. No values or almost anything stored on the client.

Retrieve the data using GetAsync or UpdateAsync and check the return value.

Demonstration:

local DataStore = game:GetService("DataStoreService"):GetDataStore("Data")

game.Players.PlayerAdded:Connect(function(player)
    local success, data = pcall(DataStore.GetAsync, DataStore, player.UserId)
    
    if success then
        if data then
              if data >= 3 then --> assuming that you have saved some number
                    --> execute code
             end
        end
    end
end)

You need to have gui on the client, Fire with a remote event/function the server to purchase the product, then you can fire back weather the purchase was successful or not

Yeah this is the reason I don’t store the data on the client.

1 Like

You can just use a remote function to return whether it was successful or not. Also, @rivertropic if it is just inside a data store you should make a value to hold how much they have while in the game and then save that value to the data store after leaving.

Exploiters could change this value.

No they could not. Just make it on the server and check on the server as well.

Ah, I thought you meant on the client.

My current code for the server on products is:

local DataStoreService = game:GetService("DataStoreService")
local frCashData = DataStoreService:GetDataStore("frCashBETAThree")



function CheckFr(userid)
	local success, frCash = pcall(function()
		return frCashData:GetAsync("player_"..userid)
	end)
	
	if success then
		-- Send data to client, wait for response for how much product costs then check if they have enough
	end
end


How would I add this?

1 Like

What are you saving though? Also check if frCash is nil and if it is, send the default data to the client.

2 Likes

No. Just invoke a remote function on the client telling the server to buy and all that, and then return if it was successful or not.

game.ReplicatedStorage.BuyEvent:InvokeServer(itemName)

On the server:

local prices = {
   ["Item"] = 30;
}

game.ReplicatedStorage.BuyEvent.OnServerInvoke = function(plr, itemName)
          local statsFolder = plr:WaitForChild("leaderstats")
          local cash = statsFolder:WaitForChild("Cash")
          
          if cash.Value >= prices[itemName] then
                cash.Value -= prices[itemName]
                --Code for giving item

               return true --for telling the player that it was purchased
         end
end

The cash isn’t a leaderboard value as it is more of an account balance than a game cash (like simulators etc)

image

1 Like

So then just change the cash to the value that it is.

What do you mean? :sweat_smile: Sorry if I seem dumb lol.

1 Like

Change this line:

To whatever your cash value for the player is. If it is held inside a table, index it that way.

Okay, I’ll leave it for today and work on it tommorow, thanks for the help!

1 Like