Hello. I am seeking help with a purchase button. Currently, I have a purchase button which gives the player a specified tool, but, I’m looking into how to make it so once you click purchase, the purchase button is no longer visible. The button is not for a gamepass, it’s for an item which they can buy with their in-game currency for example, coins/cash.
Any help with this will do. Feel free to ask any questions.
Well assuming you send a remote event to prompt the purchase on the server, you can use a Remote Function to invoke that purchase gamepass request to the server, then handle the purchase on the server, if all went well and the purchase was successful, return true, as the purchase was completed (also reward them the tool). Then on the client, check if the remote function returned true, if it did, make the button invisible .Visible, if it returned false then something went wrong, so dont make it invisible.
Making it false forever is as simple as checking if they own the gamepass, if they dont then make it visible, if they do, dont.
Yep, same sort of system with the remote function, however instead use a datastore value in the player to determine if they bought the gun or not. If they buy it, set the value to true and save it. When they join, check if the value is true or false (you can use another remote function here and return the value from the server), then toggle the visibility based on the value returned
If you need some help with this I can provide some sample code, as remote functions can be a bit tedious if you haven’t used them before.
This sort of system can work for multiple guns, but you can use dictionaries to store the data instead of values.
An example of the code would be:
Purchasing Client
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local button = script.Parent -- path to your button
button.Activated:Connect(function()
local purchaseEvent = ReplicatedStorage.YourRemoteFunction:InvokeServer(button.Name)
-- Invoke the button name, as you want to get what gun they
-- want to buy on the server
if purchaseEvent == true then -- success
button.Visible = false -- we know they own it now
elseif purchaseEvent == "InsufficientFunds" then
print("Not enough cash")
else
print("Error purchasing weapon!") -- Error, see server console (most likely invalid funds)
end
end)
Server Purchase:
local DataStoreService = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local playerData = {}
local weaponPrices = {
["AK47"] = 100,
["M4"] = 200
}
ReplicatedStorage.YourRemoteFunction.OnServerInvoke = function(player, weaponToPurchase)
if weaponPrices[weaponToPurchase] then
-- We know it's a valid weapon
if player.leaderstats.Cash.Value >= weaponPrices[weaponToPurchase] then
-- Here is where your going to datastore the value
-- ONLY MINUS THE CASH IF THE DATASTORE PCALL RETURNS TRUE
player.leaderstats.Cash.Value -= weaponPrices[weaponToPurchase]
return true -- success (make sure to use a pcall with the datastore, if it doesn't save, don't minus the cash and don't save!)
else
return "InsufficientFunds" -- Return a specific identifier to determine they have no funds
end
else
return false -- Error, invalid weapon
end
end
For saving the data, you can follow this guide, as it makes more sense - if you struggle you can go with the basic datastore tutorial, and save values.
Check if they own the item when the LocalScript runs, if they do, simply set the visibility to false.
(I would run a loop for every purchase when they join)
also change the button visibility when a purchase succeeds(I would wait for a RemoteFunction response to tell me if the purchase request was a success)