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 6 years ago
Edited 6 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:

--//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 answers

Log in to vote
0
Answered by 6 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.

--//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.

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 — 6y
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 — 6y
Ad
Log in to vote
0
Answered by
Filipalla 504 Moderation Voter
6 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 — 6y
0
if thats not the problem i have no idea Filipalla 504 — 6y

Answer this question