Why does my Random Gift shop script sometimes charge multiple times?
Asked by
7 years ago Edited 7 years ago
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:
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
03 | local Players = game:GetService( "Players" ) |
04 | local Player = Players.localPlayer |
06 | local Events = ReplicatedStorage:WaitForChild( "Events" ) |
08 | local UpgradesCoinsPurchase = Events:WaitForChild( "UpgradesCoinsPurchase" ) |
10 | local currencyCOINS = Player.leaderstats.Coins |
11 | local CoinsLeaderstats = Player.leaderstats.Coins |
15 | math.randomseed(tick()) |
16 | CRATESButton.MouseButton 1 Click:Connect( function () |
18 | if item 1 _event then item 1 _event:disconnect() end |
19 | item 1 _event = Item 1. MouseButton 1 Click:Connect( function () |
21 | local Item 1 Coinsbutton = script.Parent.Frame.CRATESFrame.Item 1 Frame.BuyCoins |
22 | script.Parent.Frame.CRATESFrame.Item 1 Frame.CoinsPrice.Text = 100 |
23 | Item 1 Coinsbutton.MouseButton 1 Click:Connect( function () |
25 | local descText = script.Parent.Frame.CRATESFrame.Item 1 Frame.DescriptionFrame.ItemDescription |
26 | local random = math.random( 1 , 200 ) |
29 | if debounce = = false and Player.leaderstats.Coins.Value > = amount then |
31 | if random > = 1 and random < = 10 then |
32 | local Item 01 = Player.Inventory.HasItem 01 Blue |
33 | if Item 01. Value = = false then |
34 | UpgradesCoinsPurchase:FireServer(Item 01 , amount) |
35 | descText.Text = 'You received Item01Blue! Check your Blue Inventory!' |
36 | elseif Item 01. Value = = true then |
37 | descText.Text = 'You received Item01Blue! You already own this item. Try again!' |
41 | elseif random > = 11 and random < = 20 then |
42 | local Item 01 = Player.Inventory.HasItem 01 Green |
43 | if Item 01. Value = = false then |
44 | UpgradesCoinsPurchase:FireServer(Item 01 , amount) |
45 | descText.Text = 'You received Item01Green! Check your Blue Inventory!' |
46 | elseif Item 01. Value = = true then |
47 | descText.Text = 'You received Item01Green! You already own this item. Try again!' |
This is the script for the Remote Event which handles purchases.
02 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
04 | local Events = ReplicatedStorage:WaitForChild( "Events" ) |
05 | local UpgradesCoinsPurchase = Events:WaitForChild( "UpgradesCoinsPurchase" ) |
07 | UpgradesCoinsPurchase.OnServerEvent:Connect( function (Player, Value, Amount) |
08 | Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - Amount |
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