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
You're missing quite a few end
s.
The problem is that you have nested click events. Sometimes they'll trigger once and other times they'll trigger multiple times. To solve your problem reduce the number of click events to one if possible. Or, have them outside of each other.
Please indent next time.
--//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() -- 1st click event if item1_event then item1_event:disconnect() end item1_event = Item1.MouseButton1Click:Connect(function() -- 2nd click event local Item1Coinsbutton = script.Parent.Frame.CRATESFrame.Item1Frame.BuyCoins script.Parent.Frame.CRATESFrame.Item1Frame.CoinsPrice.Text = 100 Item1Coinsbutton.MouseButton1Click:Connect(function() -- 3rd click event 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 end -- Missing this end if random >= 1 and random <= 10 then local Item01 = Player.Inventory.HasItem01Blue if not Item01.Value then UpgradesCoinsPurchase:FireServer(Item01, amount) descText.Text = 'You received Item01Blue! Check your Blue Inventory!' else 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 not Item01.Value then UpgradesCoinsPurchase:FireServer(Item01, amount) descText.Text = 'You received Item01Green! Check your Blue Inventory!' else descText.Text = 'You received Item01Green! You already own this item. Try again!' end wait(3) end -- Missing this end end) -- Missing this end end) -- Missing this end end) -- Missing this end --etc. etc.
Without looking at how you laid out the GUI I can't help any further.
Changing CRATESButton.MouseButton1Click:Connect(function()
to CRATESButton.MouseButton1Down:Connect(function()
should do the job since mouse click often fire multiple times a click depending on the length of the click