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

Instance of "hint" not removing?

Asked by 5 years ago

As simple as this code is. I can't seem to find out why it won't work.

script.Parent.Touched:Connect(function()
msg = Instance.new("Hint", workspace)
msg.Text = "Hello It's Me!"
wait(5)
msg:Remove()
end)

I touch the part, it fires an event and the message pops up in workspace. After 5 seconds it doesn't remove itself though...

0
Remove() is deprecated in favor of Destroy() DinozCreates 1070 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You are using a global variable which is part of the problem. The other part to this problem is that the Touched event will run each time something touches it.

With both of these problems it is possible to override the global variable leaking Hint instances.

Scenario:-

  • Touched event run
  • Create new instance and set it to the global variable wait 5
  • Touched event run
  • Create new instance and set it to the global variable overwriting the existing Hint instance
  • (wait 5 end)
  • Destroy msg
  • Destroy the same msg

To resolve this you can use a debounce to prevent the event from running the function again while it is being used in another thread (each event call is a new thread in Roblox).

Using local variables would also solve this problem as the variable would only be accessible from within the function itself and each call to this function would have its own new set of variables created. In this case you only have one which holds the Hint instance.

There are other options such as the Debris service but on this level it does not really matter how you delete the Hint instance (but do not use Remove).

To solve this we would just add a debounce and make the variable local as it is good practice.

local deb = false -- debounce is accessible outside the function so it is shared
script.Parent.Touched:Connect(function()
    if deb then return end -- prevent calls from running this code
    deb = true -- enable debounce

    -- create Hint 
    local msg = Instance.new("Hint", workspace)
    msg.Text = "Hello It's Me!"
    wait(5)
    msg:Destroy() -- pls use Destroy

    deb = false -- reset
end)

Hope this helps.

Ad
Log in to vote
0
Answered by
stef0206 125
5 years ago
Edited 5 years ago

Try this!

ready = true

script.Parent.Touched:Connect(function()
    if ready == true then
        ready = false
        msg = Instance.new("Hint", workspace)
        msg.Text = "Hello It's Me!"
        wait(5)
        msg:Destroy()
        ready = true
    end
end)

Hope this helps! All I did was Remove() To Destroy() and add a debounce so the script can only fire once it's finished

Answer this question