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

Text Debounce doesn't seem to work?

Asked by
yoshi8080 445 Moderation Voter
8 years ago
Edited 8 years ago

I was making a little thing that talks once clicked, however the debounce to prevent the player from spam click and messing up the text isn't working? It works of course without any errors

StringMessages = {'Hello My Name Is Bob','I am a lovely NPC','What about you?'}
Messages = 0
Sound = script.Parent.Sound

typing = false

function NextMessage()
    Messages = Messages + 1
    if Messages > #StringMessages then
        Messages = 1
        end
    Length = string.len(StringMessages[Messages])
end

script.Parent.ClickDetector.MouseClick:connect(function()
    if not typing then
        typing = true
            NextMessage()
    for i=1,Length do
    script.Parent.NameGUI.Tex.Text = (string.sub(StringMessages[Messages],1,i))
        Sound:Play()
    wait(.25)
        end
    end
        typing = false
end)

1 answer

Log in to vote
2
Answered by
einsteinK 145
8 years ago

You got this structure:

function MouseClick()
    if not typing then
        -- do stuff
        typing = false
    end
    typing = false
end

-- First click: typing is set to false and the for-loop runs
-- Second click: if-statement is skipped, it does the "typing = false"
-- Third click: typing is false, so if-statement works again

Solution: Putting the "typing = false" inside the if-statement:

script.Parent.ClickDetector.MouseClick:connect(function()
    if not typing then
        typing = true
        NextMessage()
        for i=1,Length do
            script.Parent.NameGUI.Tex.Text = (string.sub(StringMessages[Messages],1,i))
            Sound:Play()
            wait(.25)
        end
        typing = false
    end
end)

0
Thanks, it was this simple mistake I didn't catch ;) yoshi8080 445 — 8y
Ad

Answer this question