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

Need help with this script - key pressing. Script doesn't not work as intended?

Asked by 4 years ago
Edited 4 years ago

This script function properly until it gets akeypressed = false

Q keeps printing despite releasing the Key "q"

d = game:GetService("UserInputService")

local akeypressed = true

d.InputBegan:Connect(function(input,gameProccesedEvent) local akeypressed = true

if input.KeyCode == Enum.KeyCode.Q then

    while akeypressed == true do
            wait()
            print ("q")
    end
end

end)

d.InputEnded:Connect(function(input,gameProccesedEvent)

if input.KeyCode == Enum.KeyCode.Q then

    if d.InputEnded then

         akeypressed = false
    end


end

end)

1 answer

Log in to vote
0
Answered by 4 years ago

Hey, so first, you shouldn't put the wait time at the beginning of the loop because it gives it a small fraction of time before completely breaking the loop after akeypressed = false

d.InputBegan:Connect(function(input,gameProccesedEvent) 
    if input.KeyCode == Enum.KeyCode.Q then akeypressed = true
        while akeypressed 
        do 
            print("q")
            wait()
        end
    end
end)

On the second function,

d.InputEnded:Connect(function(input,gameProccesedEvent)
    if input.KeyCode == Enum.KeyCode.Q then
            print("False")
            akeypressed = false
    end
end)

I optimized it, removing the if d.InputEnded then since I think it's not necessarily needed. So, assuming you wanted it to print out Q when the key is pressed down and stop when the key is released. Hope this answers your question!

Ad

Answer this question