I have a brick that produces a message. Here is the script:
function RemindLosers(person) msg = Instance.new ("Message") msg.Parent = game.Workspace msg.Text = "Don't forget to thumbs up! I'm talking to you," ..person.Name wait(3) msg:remove() end script.Parent.ClickDetector.MouseClick:connect(RemindLosers)
It works perfectly, but if someone spamclicks it, they don't go away.
Any solutions?
LordDragonZord's approach solves the problem indirectly by not allowing there to be more than one Message at a time.
The actual problem here is that your msg
variable is being over-written each time this function runs. To fix this, simply make the variable local:
function RemindLosers(person) local msg = Instance.new ("Message", workspace) -- The second argument is the Parent. msg.Text = "Don't forget to thumbs up! I'm talking to you, " .. person.Name wait(3) msg:Destroy() -- Remove is deprecated. end script.Parent.ClickDetector.MouseClick:connect(RemindLosers)
What this does is creates a new msg
variable for every time the function is called - they will never overlap. This is called declaring the scope of the the variable. It will not be accessible outside of the function, and multiple function calls will have their own, unique version of it, even though the same code is running.
function RemindLosers(person) msg = Instance.new ("Message") msg.Parent = game.Workspace msg.Text = "Don't forget to thumbs up! I'm talking to you," ..person.Name script.Parent.ClickDetector.MaxActivationDistance = 0 --Turn the MaxActivationDistance to 0 so they can't click it anymore. wait(3) msg:remove() script.Parent.ClickDetector.MaxActivationDistance = 32 --Default ActivationDistance end script.Parent.ClickDetector.MouseClick:connect(RemindLosers)
Just make it so they can't activate the click detector by turning the MaxActivationDistance to 0. This will prevent spamming.
With Debounce:
local debounce = false function RemindLosers(person) if not debounce then debounce = true msg = Instance.new ("Message") msg.Parent = game.Workspace msg.Text = "Don't forget to thumbs up! I'm talking to you," ..person.Name wait(3) msg:remove() debounce = false end script.Parent.ClickDetector.MouseClick:connect(RemindLosers)