Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my purchase event do multiple purchases?

Asked by 6 years ago

Hello!

Weird thing. I have a Shop with multiple shop windows -Shop A, Shop B, etc., with one RemoteEvent that handles the purchases.

If you make a purchase in Shop A, you're charged once. if you close Shop A and open Shop B to make a purchase, you're charged twice. Close Shop B and open Shop C, you're charged thrice. etc. etc. The player does receive the item - or in this case a BoolValue is set to True - but I don't want them to be charged multiple times for closing one Shop and opening another one.

I've narrowed down the problem, I think, to my RemoteEvent Script in ServerScriptStorage:

--//Get Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--//Event
local Events = ReplicatedStorage:WaitForChild("Events")
local UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase")

UpgradesCoinsPurchase.OnServerEvent:Connect(function(Player, Value, Amount)
Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - Amount --Subtract money from the player
        print(Value)
        Value.Value = true
end)

Here is what the Buy Button box Local Script looks like, I cut out the irrelevant stuff. The Value, ItemPurchased, and ItemTrackName values are changed by pressing a button in each shop:

--//Get Services
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local currencyCOINS = Player.leaderstats.Coins

--//EventFolder
local Events = RS:WaitForChild("Events")
local UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase")

--//SHOP FRAME Variables
local PurchaseFrame = Frame.PurchaseFrame
local BuyButton = PurchaseFrame.BuyButton
local Price = PurchaseFrame.Price

local ItemTrackName = RS:WaitForChild("ItemTrackNameShop")
local ItemPurchased = RS:WaitForChild("ItemPurchased")


BuyButton.MouseButton1Click:Connect(function()
MenuClickSound:Play()
    local amount = Cost
    local Value = ItemPurchased.Value
    if Player.Inventory[Value].Value == true then
        wait(3)
        else
    if Player.leaderstats.Coins.Value >= amount then
        UpgradesCoinsPurchase:FireServer(Player.Inventory[Value], amount)
        wait(1)
        PurchaseClickSound:Play()
        wait(3)     
        PurchaseFrame.Visible = false
        wait(1)
    else
        local coinsrequired = amount - currencyCOINS.Value
        wait(5)
        PurchaseFrame.Visible = false
    end
    end
end)

Any thoughts? I've tried creating multiple RemoteEvents to handle each shop's transactions, but that only caused duplicate purchases. I tried putting each shop's windows purchases in its own Local Script, but that made no difference. My next thought is I need to put all 120 items for purchase in a single window, or create a separate Shop GUI for reach Shop.

I am open to suggestions in the meantime. :D

Thank you!

0
I recommend in the future to handle stuff like the player having enough money, or other stuff like that, in the serverscript. This is because if someone finds out about the remote event and uses an exploit program, they could fire that event at their leisure and be able to buy as many stuff as they want regardless of the amount of money they own. Rule of thumb: NEVER trust the client. YaBoiToasterLord 80 — 6y
0
What are those wait() for awesomeipod 607 — 6y
0
The wait() are there due to allowing the text boxes to update with messages. I cut out the parts that weren't relevant to the problem. :) Never2Humble 90 — 6y

Answer this question