Local variable is very unpredictable

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)

Move this client event outside the while loop. Multiple connections can possibly be made and stack up.

1 Like

I would except then that script gets stuck in that while loop and never updates its local variable and if I get rid of the while loop, the image doesn’t clone again once past 3 images.

Then you can make it unstuck by using a seperate thread using coroutine or spawn.

Or you can move the event behind the while loop:

	event.OnClientEvent:Connect(function(player)
		num = num - 1
	end)	

while true do
	while num < 3 do
--stuff
	end
	wait()
end

or

coroutine.wrap(function()
while true do
	while num < 3 do
--stuff
	end
	wait()
end
end)()
--then the event
	event.OnClientEvent:Connect(function(player)
		num = num - 1
	end)	

1 Like

Thank you so much! I can finally move on.