First of all, the Activated event of the TextButton doesn't return the player that activated it:
https://developer.roblox.com/en-us/api-reference/event/GuiButton/Activated
Second of all, your first conditional statement checks to see if the player has exactly 500 cash, not if they have 500 cash or greater.
Third of all, your second conditional statement checks to see if they have less than or equal to 500 cash, meaning if they have 500 cash, they wouldn't be able to purchase it.
Fourth of all, you're doing all of this on the client, when you should always handle stuff like buying things and giving players tools on the server. And changing your money or giving yourself a tool from the client wouldn't replicate anyways, meaning other players wouldn't be able to see when the player subtracts money from their own balance or gives themselves a tool.
If you want to initiate a transaction on the client, and process it with the server, you'll have to set up a RemoteEvent.
Create a new RemoteFunction and put it inside of ReplicatedStorage. Change its name to "Purchase."
ReplicatedStorage is where you put things that the server and the player can see. The player needs to see it to invoke it, and the server needs to see it to pick up on when the player invokes it.
Put this code inside of your LocalScript:
local replicatedStorage = game:GetService("ReplicatedStorage")
local purchaseRemote = replicatedStorage.Purchase
local button = script.Parent
local frame = button.Parent
button.Activated:Connect(function()
local successfulPurchase = purchaseRemote:InvokeServer()
end)
Whenever the player clicks on the button, the player calls the InvokeServer function on the Purchase RemoteFunction.
Create a Script and put it inside of ServerScriptService. Also, put your crossbow in ServerStorage, and not in ReplicatedStorage. There is no reason for the crossbow to be replicated to the client if the client hasn't yet purchased it.
Put this code inside of that script:
local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")
local purchaseRemote = replicatedStorage.Purchase
local crossbow = serverStorage.Crossbow
purchaseRemote.OnServerInvoke = function(player)
local starterGear = player:FindFirstChild("StarterGear")
local backpack = player:FindFirstChild("Backpack")
local leaderstats = player:FindFirstChild("leaderstats")
if (leaderstats) then
local cash = leaderstats:FindFirstChild("Cash")
if (cash) then
if ((cash.Value >= 500) and (not starterGear:FindFirstChild("Crossbow"))) then
cash.Value -= 500
local backpackCrossbow = crossbow:Clone()
local starterGearCrossbow = crossbow:Clone()
backpackCrossbow.Parent = backpack
starterGearCrossbow.Parent = starterGear
return true
end
end
end
end
This script detects when a player initiates a transaction, and handles it. If they have enough money to buy the crossbow, and they don't already have it, subtract 500 cash from their money, and give it to them. StarterGear is where you put tools that you want players to be automatically given when they respawn. You put one clone of the crossbow in there, and another in their backpack, so that they can start using it right away. If you just put it in their StarterGear, they wouldn't be able to use it until they died and respawned. If you just put it in their Backpack, they'd only have it until they died. If the transaction is successful, return true. if it isn't, return false.
This takes us to the second part of the LocalScript:
local replicatedStorage = game:GetService("ReplicatedStorage")
local purchaseRemote = replicatedStorage.Purchase
local button = script.Parent
local frame = button.Parent
button.Activated:Connect(function()
local successfulPurchase = purchaseRemote:InvokeServer()
if (not successfulPurchase) then
button.Text = "You need 500 cash to unlock"
wait(2)
button.Text = "Crossbow"
end
end)
The variable "successfulPurchase" is going to be equal to whatever the server script returns. The server script will return true if the purchase was successful. If it wasn't, it'll return nil, which just means nothing was returned.
if (not successfulPurchase) then
"not successfulPurchase" will evaluate to true if the purchase was unsuccessful, which will then allow the button to display the text "You need 500 cash to unlock."
1 comment
your only firing the first "if then" if the player has exactly 500 cash :p