Multiple Machines Sometimes Break

My script works fine, that is until multiple people use it at the same time while debounce is true, I believe that’s the case.
So, basically what happens is if multiple people try to use one of the machines, it can break and no longer accept any other cup.
Note, coffeMachineVisualizer is a function that makes you wait 3 seconds + Heartbeat before debounce is false.
How can I make it work consistently?
Edit: why isn’t anyone helping me

for _, v in pairs(coffeeFolder:GetDescendants()) do
	if v.Name == "Hitbox" then
		v.Touched:Connect(function(hitPart)
			if not debounce and hitPart.Parent:IsA('Tool') then
				debounce = true
				if hitPart.Parent.Name == "Cup" then
					hitPart.Parent.Name = v.Parent.Name..' Coffee'
					coffeeMachineVisualizer(v, hitPart)
					visualEffects(hitPart.Parent, v)
				elseif hitPart.Parent.Name == "Hot Milk" then
					if v.Parent.Name == "Espresso" then
						hitPart.Parent.Name = "Cappuccino"
					elseif v.Parent.Name == "Regular" then
						hitPart.Parent.Name = "Frappuccino"
					end
					
					coffeeMachineVisualizer(v, hitPart)
					visualEffects(hitPart.Parent, v)
				end
				RunService.Heartbeat:Wait()
				debounce = false
			end
		end)
	end
end

Just as you mentioned its most likely due to the fact that you have 1 debounce that every machine uses and each machine is setting it to true/false at different intervals, etc. You should have your debounce within the loop right after if v.Name == "Hitbox" then.

That would basically give each machine its “own” debounce. Hopefully that solves the issue!

1 Like

What do you mean?

local coffeeDebounce = false

?

you just won me everything and brand new kowledge, thanks brother

Your variable debounce is outside the for loop so every machine is setting the same debounce, etc.

E.g:
if not debounce and hitPart.Parent:IsA('Tool') then

Opps sent that a little to late but glad you got it!

I have multiple singular touch functions in the same script such as this, would I have to make different variable debounces? like global debounces at the top of the script?

cookingObjectFolder.Milk.Hitbox.Touched:Connect(function(hitPart)
	if not debounce and hitPart.Parent:IsA('Tool') then
		if hitPart.Parent.Name == "Cup" then
			debounce = true
			hitPart.Parent.Name = "Milk"
			visualEffects(hitPart.Parent, cookingObjectFolder.Milk.Hitbox)
			RunService.Heartbeat:Wait(hb)
			debounce = false
		end
	end
end)

Is that the full script and are you looping through it and connecting the touch events?

If not than it should be fine. I also suggest giving your debounces specific names. Such as CupDebounce for your cup to milk machine, etc. That way you don’t end up having two separate touch connections altering the same debounce.

1 Like