Answered by
6 years ago Edited 6 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.
02 | script.Parent.Touched:Connect( function () |
03 | if deb then return end |
07 | local msg = Instance.new( "Hint" , workspace) |
08 | msg.Text = "Hello It's Me!" |
Hope this helps.