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