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)
end
end)
end)
end)
--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

6 comments
Delete line 11, you don't need it.
Thanks for the response, I must have it used elsewhere in the script as a whole, but you're correct that I do not need it here. :) do you have any advice for my problem itself?
Why not disconnect item1_event after the remoteEvent is fired? Also might want to work on your syntax and fix the errors to make it easier for others to read.
@Meltdown81 - To make sure I understand you clearly, you're suggesting I remove "item1_event:disconnect() end" from Line 18 and place it after Line 34, and after Line 44, or after Line 54? What errors are you seeing? o.o The script works just fine. I understand people have preferences on how a code is organized, and I indented mine here for visibility, but I don't personally mind if its all laid-