Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to fix this problem?

I have a brick that produces a message. Here is the script:

function RemindLosers(person)
msg = Instance.new ("Message")
msg.Parent = game.Workspace
msg.Text = "Don't forget to thumbs up! I'm talking to you," ..person.Name
wait(3)
msg:remove()

end

script.Parent.ClickDetector.MouseClick:connect(RemindLosers)

It works perfectly, but if someone spamclicks it, they don't go away.

Any solutions?

2 answers

LordDragonZord's approach solves the problem indirectly by not allowing there to be more than one Message at a time.

The actual problem here is that your msg variable is being over-written each time this function runs. To fix this, simply make the variable local:

function RemindLosers(person)
	local msg = Instance.new ("Message", workspace) -- The second argument is the Parent.
	msg.Text = "Don't forget to thumbs up! I'm talking to you, " .. person.Name
	wait(3)
	msg:Destroy() -- Remove is deprecated.
end

script.Parent.ClickDetector.MouseClick:connect(RemindLosers)

What this does is creates a new msg variable for every time the function is called - they will never overlap. This is called declaring the scope of the the variable. It will not be accessible outside of the function, and multiple function calls will have their own, unique version of it, even though the same code is running.

3 comments

0

adark, this script u made was worse than mine. The messages it creates have no text!

0

What's wrong with :remove()? I know :Destroy() is better and most people use it, but what if you need it again later? :remove() changes the parent of an object to nil so I could retrieve it later if I wanted to, and it's shorter than writing "script.Parent = nil".

0

That's true, but in this case it's unneccesary, and storing stuff in nil is bad practive regardless. Garbage collection exists. @groovy; I don't know why you're not getting any text with this one, I just tested it and it works fine for me.

function RemindLosers(person)
msg = Instance.new ("Message")
msg.Parent = game.Workspace
msg.Text = "Don't forget to thumbs up! I'm talking to you," ..person.Name
script.Parent.ClickDetector.MaxActivationDistance = 0 --Turn the MaxActivationDistance to 0 so they can't click it anymore.
wait(3)
msg:remove()
script.Parent.ClickDetector.MaxActivationDistance = 32 --Default ActivationDistance
end

script.Parent.ClickDetector.MouseClick:connect(RemindLosers)

Just make it so they can't activate the click detector by turning the MaxActivationDistance to 0. This will prevent spamming.

With Debounce:

local debounce = false
function RemindLosers(person)
if not debounce then
debounce = true
msg = Instance.new ("Message")
msg.Parent = game.Workspace
msg.Text = "Don't forget to thumbs up! I'm talking to you," ..person.Name
wait(3)
msg:remove()
debounce = false
end

script.Parent.ClickDetector.MouseClick:connect(RemindLosers)

2 comments

0

A debounce is good, too.

0

@Red I added a script with debounce in it.

This discussion is preserved as it was. New answers, comments, and votes are closed.