The way you're scripting and explaining what your goal is, the script doesn't yield until the click detector is clicked, then wait for one second to remove the message. The script will never remove the message you're referring to.
A function is dormant until it is called upon by an event. With that being said, you can have the function itself create a message, then have it destroyed in the same scope. That way, it would function as intended instead of possibly deleting a nonexistent object, or an object named as "Message."
function onClicked()
local takeoff = Instance.new('Message', Workspace)
takeoff.Text = "Testing"
wait(1)
takeoff:Remove()
end
script.Parent.MouseButton1Click:connect(onClicked)
I would like to introduce to you a concept you may have seen before: debounce.
It makes sure that the function you're intending to make it do can only be executed one at a time. A typical debounce example looks like this:
debounce = false
function Example()
if debounce == false then
debounce = true
debounce = false
end
end
It starts off as a global, initialized variable. Then it sees if the debounce is what it sets to (in this case, false). Then it will be reassigned as true.
The thing is, once somebody clicks the click detector again, the function will not do anything, as debounce is true and the "if" statement's content can only be executed if debounce is false. Once everything in the "if" statement is executed, then debounce will be set back to false.
This concept is applicable to things that pauses the script (such as wait()). You can do it in instantaneous situations too, such as changing a property. But that's not very feasible, as it is instantaneous and the script won't look too clean and efficient.
What does it have anything to do with your message function? Well, if you have 20 people clicking the click detector 10 times, then you'd have 200 messages in the Workspace. Which doesn't sound very feasible. Of course, it will be deleted a second after the last click. But it is useful in other applications, such as making the door create a fade effect.
Now, how to implement that in your function?
debounce = false
function onClicked()
if debounce == false then
debounce = true
local takeoff = Instance.new('Message', Workspace)
takeoff.Text = "Testing"
wait(1)
takeoff:Remove()
debounce = false
end
end
script.Parent.MouseButton1Click:connect(onClicked)
Everytime the onClicked function is executed and if debounce is false, then debounce will be set to true and the rest of the function is executed. It won't be executed again until debounce is set to false once again.
5 comments
Um, as brilliant as that may be, it still didn't fix the issue. Maybe I can meet you on roblox and you can try it for yourself. The issue is not too many people clicking it, it is just not going away even with the debounce. I also checked workspace when I am testing it and it shows only 1 message got created.
Debouncing is an optional thing to implement. You don't have to make it do so. All you have to do is put the
wait(1)andtakeoff:Remove()lines inside of the function. That should remove the message. And you say that only 1 message got created? That's what debouncing does; it execute things one at a time.Champion, you attempted to remove an object that you had no logical reason to assume existed. That's your error. Everything else is optional.
@Perci - Didn't work... Even as a localscript, using his version.
Tip: Localscripts can only run in the player's backpack, character model, or the player's player gui. It will not work in workspace.