Hello!
What are you attempting to achieve?
I have a “Mystery Crate” shop purchase system that when the button is clicked, will check if a player has enough coins, if so it will charge them and then choose a random number to determine what Item is being granted, and then will check if the player already has the Item, if the player already owns the Item it will grant another Item instead.
What is the issue?
Sometimes, randomly, it will charge multiple times for a single purchase, and award multiple items. This is undesirable as the player may only wish to purchase one item. But most of the time it works correctly, one click = one purchase/item granted.
What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)
I’ve re-configured my script a few times, but I don’t receive error outputs, and I can’t find a pattern for what triggers the multi-purchases.
Here is an abbreviated form of my script below, 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
-- This is the Remote Event which handles the purchases:
local UpgradesCoinsPurchase = Events:WaitForChild("UpgradesCoinsPurchase")
item1_event = nil
item2_event = nil
--more events for more items.
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 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 Inventory!'
elseif Item01.Value == true then
descText.Text = 'You received Item01Green! You already own this item. Try again!'
end
wait(3)
-- an additional 18 options below that mirror the above elseif statement except for the random numbers and the Item received being adjusted.
end
end
end)
end)
--repeat with additional Item events that mirror the above, except instead of item1_event its item2_event, etc.
This is my Remote Event script inside ServerScriptStorage:
--//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)
The print of the Value purchase does confirm that more than one item was granted and charged for.
Any insight or directions to go would be much appreciated. Thanks!