This function (button.MouseButton1Up:Connect) is running more and more times for every iteration of the (openItem) function that calls it. This means that every time openItem is run, it will count as 2 Mouse Button presses, and then 3, and then 4, 5, etc.
I’ve tried putting debounces but they don’t seem to be working.
function openItem(plr, debounce)
local theGet = game:GetService("ReplicatedStorage"):WaitForChild("GameItems"):FindFirstChild(Tool)
local theItem = require(theGet.ItemConfig)
local leader = plr:WaitForChild("leaderstats")
local gold = leader:WaitForChild("Oofs")
local gui = plr:WaitForChild("PlayerGui"):WaitForChild("ItemInfo"):WaitForChild("GUI")
local initialPosition = plr.Character.HumanoidRootPart.Position
local buyButton = gui:FindFirstChild("Buy")
local info = gui:WaitForChild("Info")
info.Text = theItem.Info
local iname = gui:WaitForChild("Name")
iname.Text = Tool
local damage = gui:WaitForChild("Damage")
damage.Text = ""
local rarity = theItem.Rarity
if rarity == "Common" then
iname.TextColor3 = Color3.fromRGB(255, 255, 255)
elseif rarity == "Uncommon" then
iname.TextColor3 = Color3.fromRGB(85, 255, 127)
elseif rarity == "Rare" then
iname.TextColor3 = Color3.fromRGB(114, 128, 255)
elseif rarity == "Legendary" then
iname.TextColor3 = Color3.fromRGB(255, 204, 0)
end
local damageImage = gui:WaitForChild("Sword")
damageImage.Visible = false
local CritImage = gui:WaitForChild("CritImage")
CritImage.Visible = false
local HealImage = gui:WaitForChild("HealImage")
HealImage.Visible = true
local healInfo = gui:WaitForChild("HealInfo")
healInfo.Text = theItem.HealAmount
local critchance = gui:WaitForChild("CritChance")
critchance.Text = ""
local cost = gui:WaitForChild("Cost")
cost.Text = Cost
local oofs = gui:WaitForChild("Oofs")
oofs.Text = gold.Value
local iimage = gui:WaitForChild("AImage")
iimage.Image = Image
disableUI:FireClient(plr)
gui.Parent.Enabled = true
local button = gui:FindFirstChild("Buy")
button.MouseButton1Up:Connect(function()
if debounce == false then return end
debounce = false
if plr:WaitForChild("StarterGear"):FindFirstChild(Tool) then
gui:FindFirstChild("Buy").Text = "Already Own."
gui:FindFirstChild("Buy").TextColor3 = Color3.fromRGB(47, 255, 0)
return
end
if gold.Value >= Cost then
buyItem(plr)
debounce = false
else
gui:FindFirstChild("Buy").Text = "Not Enough!"
gui:FindFirstChild("Buy").TextColor3 = Color3.fromRGB(255, 0, 4)
end
wait(1)
debounce = true
wait(1)
gui:FindFirstChild("Buy").Text = "Buy"
gui:FindFirstChild("Buy").TextColor3 = Color3.fromRGB(0,0,0)
end)
function buyItem(plr)
gold.Value = gold.Value - Cost
--local CopiedItem = game:GetService("ReplicatedStorage"):WaitForChild("GameItems"):FindFirstChild(Tool):Clone()
--CopiedItem.Parent = plr:WaitForChild("StarterGear")
if plr.Backpack:FindFirstChild(Tool) or plr.Character:FindFirstChild(Tool) then
-- If we already have this item
plr.Character:WaitForChild("Items"):FindFirstChild(Tool).Value += 1
else
local CopiedItem2 = game:GetService("ReplicatedStorage"):WaitForChild("GameItems"):FindFirstChild(Tool):Clone()
CopiedItem2.Parent = plr.Backpack
end
local sound = Instance.new("Sound",script.Parent)
sound.SoundId = _M.SoundId
sound.Volume = 1
sound.MaxDistance = 50
sound:Play()
gui:FindFirstChild("Buy").Text = "Bought!"
oofs.Text = gold.Value
gui:FindFirstChild("Buy").TextColor3 = Color3.fromRGB(47, 255, 0)
debounce = true
end
while wait(0.1) do
local currentPosition = plr.Character.HumanoidRootPart.Position
local distance = (currentPosition - initialPosition).Magnitude
if distance > 5 then
gui.Parent.Enabled = false
break
end
end
end
script.Parent.ProximityPrompt.Triggered:Connect(function(player)
disableUI:FireClient(player)
local debounce = player.PlayerGui:WaitForChild("ItemInfo"):FindFirstChild("GUI").Buy.debounce
openItem(player, debounce)
end)
What can be done to fix this? Any help would be appreciated