function onClicked() local takeoff = Instance.new('Message', Workspace) takeoff.Text = "Testing" end script.Parent.MouseButton1Click:connect(onClicked) wait(1) game.Workspace.Message:Remove()
The message is supposed to appear when a GUI button is clicked. The message appears and displays the correct text but doesn't disappear.
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 -- This "locks" the if statement, effectively making sure that the 'stuff' is executed one at a time. Because if the debounce variable is true and the function is called again, it will meet the 'if debounce == false then' line, essentially skipping over it because true does not equal to false. -- insert stuff here 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.
I found an alternative to your problem. Instead of having two functions, let's reduce it to one like so:
textbutton = script.Parent -- The gui button debounce = true textbutton.MouseButton1Click:connect(function() -- We execute the MouseButton1Click event on the text button if debounce then debounce = false local takeoff = Instance.new('Message', Workspace) -- We create a message on Workspace takeoff.Text = "Testing" -- We change the text to "Testing" wait(1) game.Workspace.Message:Destroy() -- We destroy the message after a second. debounce = true end end)
I've tested this and it worked great. :)