Hello!
I have a random gift shop script. When the Player presses the button, a pop-up purchase menu appears with the Buy button. Once the Buy button is selected, script checks if the player has enough money, if so, it will then begin the random number process, "if between 1 and 10 then" it gives the item. If a player already has the item, it moves on to the next item, this way, no item is awarded twice.
Only problem here is occasionally, it will award, and charge, more than once, per button press. This happens seemingly randomly. I'd rather this not happen. Here is an abbreviated version of the script, which is a Local Script inside a GUI:
--//Get Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Player = Players.localPlayer
--//EventFolder
local Events = ReplicatedStorage:WaitForChild("Events")
--//Events
local UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase") -- This is the Remote Event which handles the purchases.
local currencyCOINS = Player.leaderstats.Coins
local CoinsLeaderstats = Player.leaderstats.Coins
item1_event = nil
math.randomseed(tick())
CRATESButton.MouseButton1Click:Connect(function()
if item1_event then item1_event:disconnect() end
item1_event = Item1.MouseButton1Click:Connect(function()
local Item1Coinsbutton = script.Parent.Frame.CRATESFrame.Item1Frame.BuyCoins
script.Parent.Frame.CRATESFrame.Item1Frame.CoinsPrice.Text = 100
Item1Coinsbutton.MouseButton1Click:Connect(function()
local amount = 100
local descText = script.Parent.Frame.CRATESFrame.Item1Frame.DescriptionFrame.ItemDescription
local random = math.random(1,200)
local debounce = false
if debounce == false and Player.leaderstats.Coins.Value >= amount then
debounce = true
if random >= 1 and random <= 10 then
local Item01 = Player.Inventory.HasItem01Blue
if Item01.Value == false then
UpgradesCoinsPurchase:FireServer(Item01, amount)
descText.Text = 'You received Item01Blue! Check your Blue Inventory!'
elseif Item01.Value == true then
descText.Text = 'You received Item01Blue! You already own this item. Try again!'
end
wait(3)
elseif random >= 11 and random <= 20 then
local Item01 = Player.Inventory.HasItem01Green
if Item01.Value == false then
UpgradesCoinsPurchase:FireServer(Item01, amount)
descText.Text = 'You received Item01Green! Check your Blue Inventory!'
elseif Item01.Value == true then
descText.Text = 'You received Item01Green! You already own this item. Try again!'
end
wait(3)
--etc. etc.
This is the script for the Remote Event which handles purchases.
--//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)
From the Print of the Value being purchased in the Remote Event script, I can verify multiple items are being purchased/charged. But its random how often it happens. Most of the time it acts as intended and only awards/charges for a single purchase, but I can't have the random chance of it charging multiple times and sending a Player's currency into the negative numbers.
Anyone have any suggestions for how to eliminate the random purchase factor from my random gift script, or if there's a better way to script my random gift giver that also prevents duplicate gifts? ...The irony of a random problem with a random script is not lost on me. :D

2 comments
Thank you for the response! :) I have all the ends in my script proper, I wanted to abbreviate how long the script is for this segment I posted. I'll see what I can do to remove the number of click events. But the way the GUI is laid out the first click button is to open the random Crate menu. The second one is to select which mystery gift (out of thirty) you want which opens the purchase menu,