Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[NOT ANSWERED]Why won't my Message remove itself from Workspace?

Asked by 9 years ago
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.

2 answers

Log in to vote
2
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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.

0
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. Champion121212 22 — 9y
0
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)` and `takeoff: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. Redbullusa 1580 — 9y
0
Champion, you attempted to remove an object that you had no logical reason to assume existed. That's your error. Everything else is optional. Perci1 4988 — 9y
0
@Perci - Didn't work... Even as a localscript, using his version. Champion121212 22 — 9y
0
Tip: Localscripts can only run in the player's backpack, character model, or the player's player gui. It will not work in workspace. Redbullusa 1580 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

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. :)

0
Apparently it works for everyone else but not for me.... Is it some kind of roblox glitch Champion121212 22 — 9y

Answer this question