I have an image button that appears in threes. Once the image button is clicked, it disappears and another one appears. To do this, I had a local value in a script (Script 1) that determined the number of images on the screen, adding one each time an image button was cloned and another script (Script 3) inside the image button that made it disappear and fired the server (Script 2) to then fire the client which was picked up by script 1 and deducted the local variable by 1. This works perfectly fine most of the time, but some of the time, typically when I click on them in rapid succession, the number starts going into the negatives by thousands therefore making thousands of clones. I don’t know why this happens and I’ve been trying potential fixes all of 2 days.
Script 1: (Local Script)
wait()
local num = 0
local player = game:GetService("Players").LocalPlayer
local event = game.ReplicatedStorage.Jobs.FoodStore.DirtyPlate.DirtNum
local Gui = player.PlayerGui.Gui.Jobs.FoodStore.Gui
local dirt = player.PlayerGui.Gui.Jobs.FoodStore.Gui.DirtyPlate.Plate.DirtOrigin
while true do
while num < 3 do
num = num + 1
local Dirt = dirt:Clone()
Dirt.Parent = Gui.DirtyPlate.Plate
local cords1 = tonumber(math.random(1, 6) / 10)
local cords2 = tonumber(math.random(1, 6) / 10)
Dirt.Position = UDim2.new(cords1, 0, cords2, 0)
Dirt.Name = "dirt"
Dirt.Size = UDim2.new(0.3, 0, 0.3, 0)
Dirt.ImageTransparency = 1
for i = 1, 20 do
Dirt.ImageTransparency = (1 - (i / 20))
wait()
end
wait(math.random(2, 5))
end
event.OnClientEvent:Connect(function(player)
num = num - 1
end)
wait()
end
Script 2: (Script)
local event = game.ReplicatedStorage.Jobs.FoodStore.DirtyPlate.DirtNum
event.OnServerEvent:Connect(function(player)
event:FireClient(player)
wait(0.6)
end)
Script 3: (Local Script)
wait()
local player = game:GetService("Players").LocalPlayer
local dirt = script.Parent
local event = game.ReplicatedStorage.Jobs.FoodStore.DirtyPlate.DirtNum
local debounce = true
dirt.MouseButton1Click:Connect(function()
if debounce == true then
debounce = false
event:FireServer(player)
for i = 1, 20 do
dirt.ImageTransparency = (i / 20)
wait(0.03)
end
dirt:Remove()
end
end)