Hi I was making a shop script and if you rapid click it messes up. So could you help me with debounce? I need it before the script.Parent.Parent.Buy.Visible = true and script.Parent.Parent.error.Visible = true
plr = game.Players.LocalPlayer price = 550 script.Parent.MouseButton1Click:connect(function() local v = plr.leaderstats.Credits if v.Value >= price then game.Players.LocalPlayer.Backpack.ClassicSword.Configurations.LungeDamage.Value = 100 v.Value = v.Value - price script.Parent.Parent.Buy.Visible = true wait(0.1) wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.1 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.2 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.3 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.4 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.5 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.6 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.7 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.8 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 0.9 wait(0.1) script.Parent.Parent.Buy.TextTransparency = 1 script.Parent.Active = false script.Parent.Visible = false print('You bought the item!') else print ('Cant buy item!') script.Parent.Parent.error.Visible = true wait(0.1) script.Parent.Parent.error.TextTransparency = 0.1 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.2 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.3 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.4 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.5 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.6 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.7 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.8 wait(0.1) script.Parent.Parent.error.TextTransparency = 0.9 wait(0.1) script.Parent.Parent.error.TextTransparency = 1 end end)
Heres a debounce and two for loops to tidy up the code.
plr = game.Players.LocalPlayer price = 550 debounce = false script.Parent.MouseButton1Click:connect(function() if debounce then return end -- If debounce = true then stop local v = plr.leaderstats.Credits debounce = true -- When the code gets to here, stop multiple clicks if v.Value >= price then game.Players.LocalPlayer.Backpack.ClassicSword.Configurations.LungeDamage.Value = 100 v.Value = v.Value - price script.Parent.Parent.Buy.Visible = true wait(0.2) for i = 0.1,1,0.1,do -- from 0.1 to 1 with 0.1 increments wait(0.1) script.Parent.Parent.Buy.TextTransparency = i end script.Parent.Active = false script.Parent.Visible = false print('You bought the item!') else print ('Cant buy item!') script.Parent.Parent.error.Visible = true for i = 0.1,1,0.1,do -- from 0.1 to 1 with 0.1 increments wait(0.1) script.Parent.Parent.error.TextTransparency = i end end debounce = false end)