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

How to make it so a person doesn't spam the onTouched event ?(PLEASE LOOK)

Asked by 9 years ago

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)`

0
Please format your code in Lua code block. Goulstem 8144 — 9y

3 answers

Log in to vote
5
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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)
0
No point in checking hit.Parent unmiss 337 — 9y
0
Lmao, I'm very paranoid that the parent of hit could be nil. Redbullusa 1580 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Try adding and end) or end 2 times on the end

Log in to vote
-1
Answered by
Zeloi 40
9 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

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)`
0
Gives a error on the "script.Parent.Touched:connect(onTouched)" part CodedEffects 10 — 9y
0
That's because there's a stray "`" at the end of that line. It should work without that, but, Zeloi, please elaborate more on your explanations. Redbullusa 1580 — 9y

Answer this question