PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Keep Messages From Triggering

Keep Messages From Triggering

Jun 25 2018, 1:05 PM PST

Problem

You want to create a message when someone steps on a part that notifies the name of the person who stepped on the part.

Solution

Use debounce to restrict too many triggers.

local enabled = true
Workspace.Button.Touched:connect(function(hit)
	if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) and enabled == true then
		local msg = Instance.new('Message')
		msg.Text = hit.Parent.Name .. " stepped on me!"
		msg.Parent = workspace
		enabled = false
		wait(3)
		enabled = true
		msg:Destroy()
	end
end)

Discussion

In the first line I declare enabled to be the boolean (true of false value) true. I then proceed to do a stand connection to the Touched event. This time I assume there is a part in the workspace called “Button” since we did not dynamically create it.

Next, we check to see if the part’s parent is a character, with the Players/GetPlayerFromCharacter method. Say for example a character’s right leg hit the button, Instance/Parent|hit.Parent would be the character. We also check to see if enabled is true (this is where the debounce comes in).

Debounce is restricting the Touched event from triggering too many times. Say for example you jump on the part multiple times before the 3 seconds to remove the message is up. This means that many new messages will be created on top of each other. Also, the way the Roblox physics engine works, one touch is triggered as many touches so you end up getting bombarded with extra messages even for one touch.

After the conditional we create a new message, and set it up with some text and parent it to the workspace so it can be seen. Then we set enabled to false so that if the event is triggered again, the conditional won’t let it create new messages. We then wait 3 seconds and set enabled to true so that events thereafter can fire once more. The message is then removed.

Tags:
  • debounce
  • debugging
  • trigger
  • touch
  • script