Perhaps you need to know what "multi-threading" is. In programming, sometimes it is better to have several different things running at the same time. Each script you write in Lua is run on a different thread, and is basically run "at the same time", although Lua does it differently (and runs each thread one after another repeatedly). Anyway, this multi-threading allows things to be done at the same time.
So, without debounce, look at this example code (not in Lua, just summed up logic) :
OnChatted("regenerate")
wait(2)
Regenerate(MyModels)
wait(2)
message = new message
message.text = "REGENERATION COMPLETE!"
message.Parent = game.Workspace
wait(5)
message.Parent = nil
end
With that code, if 5 people said regenerate at near exact same time, MyModels would be trying to regenerate in 5 different threads! Imagine the regeneration, which removes the model - the model wouldn't even exist when the 2nd thread got to it! There would be many errors, and many messages coming up and some may not even stop - they'll be permanently on screen. Look at just 2 threads in this msg example:
Thread 1 runs "message = new message" up to "message.Parent = game.Worksapce" then runs "wait()", giving Thread2 a chance to do something.
Thread 2 runs the same thing that Thread 1 just ran, but now the variable "message" refers to a NEW message.
Thread 1 runs "message.Parent = nil", but because Thread 2 assigned a NEW message value to that variable, it takes off Thread 2's message, not its own.
Thread 2 runs "message.Parent = nil", but does nothing else because the message's parent is already NIL! So, Thread 1's original message is permanently on.
That is an example of why debounce is used - to prevent those situations from happening! |