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

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:

01--//Get Services
02local   ReplicatedStorage = game:GetService("ReplicatedStorage")
03local   Players = game:GetService("Players")
04local   Player = Players.localPlayer
05--//EventFolder
06local   Events = ReplicatedStorage:WaitForChild("Events")
07--//Events
08local   UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase") -- This is the Remote Event which handles the purchases.
09 
10local   currencyCOINS = Player.leaderstats.Coins
11local   CoinsLeaderstats = Player.leaderstats.Coins
12 
13item1_event = nil
14 
15math.randomseed(tick())
View all 51 lines...

This is the script for the Remote Event which handles purchases.

01--//Get Services
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03--//Event
04local Events = ReplicatedStorage:WaitForChild("Events")
05local UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase")
06 
07UpgradesCoinsPurchase.OnServerEvent:Connect(function(Player, Value, Amount)
08Player.leaderstats.Coins.Value = Player.leaderstats.Coins.Value - Amount --Subtract money from the player
09        print(Value)
10        Value.Value = true
11end)

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 answers

Log in to vote
0
Answered by 7 years ago

You're missing quite a few ends.

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.

01--//Get Services
02local   ReplicatedStorage = game:GetService("ReplicatedStorage")
03local   Players = game:GetService("Players")
04local   Player = Players.localPlayer
05--//EventFolder
06local   Events = ReplicatedStorage:WaitForChild("Events")
07--//Events
08local   UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase") -- This is the Remote Event which handles the purchases.
09 
10local   currencyCOINS = Player.leaderstats.Coins
11local   CoinsLeaderstats = Player.leaderstats.Coins
12 
13item1_event = nil
14 
15math.randomseed(tick())
View all 56 lines...

Without looking at how you laid out the GUI I can't help any further.

0
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, Never2Humble 90 — 7y
1
- and the third click event is the purchase button itself which triggers all the random code and the purchase. :/ I'll have to sit down and think on the best way to condense it. Never2Humble 90 — 7y
Ad
Log in to vote
0
Answered by
Filipalla 504 Moderation Voter
7 years ago

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

0
That's wrong, I tested it (because I was curieus lol) and the event fires the moment you don't click the button anymore, it has nothing to do with the lenght of the click and fires only once User#20388 0 — 7y
0
if thats not the problem i have no idea Filipalla 504 — 7y

Answer this question