How do I make this so a person can not spam the onTouched event?
`function onTouched() Instance.new("Hint", game.Workspace) wait() game.Workspace.Message.Text="Worked" wait(5) game.Workspace.Message:Remove()
end
script.Parent.Touched:connect(onTouched)`
Perhaps make sure that the object touching the part is an actual player, else any brick could touch it. ;)
function onTouched(Hit) local Player = Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health > 0 and game.Players:GetPlayerFromCharacter(Hit.Parent) if Player then local MSG = Instance.new("Hint", game.Workspace) -- Define this new instance with a variable. You'll have more control over the object you're making. wait() MSG.Text="Worked" wait(5) MSG:Destroy() -- This locks the message in "nil." ":Remove()" is deprecated. end end script.Parent.Touched:connect(onTouched)
Debounce is a very useful concept to learn. It means that you can only process one chunk of executions at a time. It's like regenerating a car (if you're an old ROBLOX player like me, you remember this ;)), the purple regenerating button turns black for a certain amount of time. Then becomes back to purple, allowing you to regenerate the car again.
The most common way of implementing this is to manipulate a static variable and wrap an "if" statement around your chunk of code.
Debounce = false function onTouched(Hit) local Player = Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health > 0 and game.Players:GetPlayerFromCharacter(Hit.Parent) if Player then if not Debounce then Debounce = true local MSG = Instance.new("Hint", game.Workspace) wait() MSG.Text="Worked" wait(5) MSG:Destroy() Debounce = false end end end script.Parent.Touched:connect(onTouched)
Debounce
debounce = false function onTouched() Instance.new("Hint", game.Workspace) if not debounce then debounce = true wait() game.Workspace.Message.Text="Worked" wait(5) game.Workspace.Message:Remove() debounce = false end script.Parent.Touched:connect(onTouched)`