Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to fix this problem?

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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.

0
adark, this script u made was worse than mine. The messages it creates have no text! groovydino 2 — 10y
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". EzraNehemiah_TF2 3552 — 10y
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. adark 5487 — 10y
Ad
Log in to vote
0
Answered by 10 years ago
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)
0
A debounce is good, too. Redbullusa 1580 — 10y
0
@Red I added a script with debounce in it. EzraNehemiah_TF2 3552 — 10y

Answer this question