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.
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
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.
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
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