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

Any easier way to write this?

Asked by
Zerio920 285 Moderation Voter
9 years ago

The Wiki says I can record key presses in a table to determine whether the key is being held or not, so something like

local keys = {}

    mouse.KeyDown:connect(function(key)
        keys[key] = true
    end)
    mouse.KeyUp:connect(function(key)
        keys[key] = nil
    end)

    while wait(0.1) do
        if keys["q"] and Tool.Val.Value > 0 then
            Tool.Val.Value = Tool.Val.Value - 5
        elseif keys["e"] and Tool.Val.Value < 200 then
            Tool.Val.Value = Tool.Val.Value + 5
        end 
    end

However, I'd also like to differentiate between a key press and someone holding down the key, and while the script is effective enough at doing that, it only checks for key presses every 1/10th of a second, so anytime someone pushes a key in that 10th of a second it won't count. It's minor, yeah, but I'd really like to find a more concise method of determining keystrokes from key presses.

1 answer

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I don't understand why you're worried. The worst that can happen is you press a key and there's a 0.1 second delay between you pressing the key and the value changing. If someone actually manages to press and release a key in less than one tenth of a second, then you're right, it won't register. It is very unlikely that this would happen, however.

You can make the loop go faster if you want. If you leave out the argument in wait() it will delay the script the minimum amount of time it can, about a 30th of a second.

1
Since this is a LocalScript, RenderStepped could be used as well, which is the *true* minimum wait time on the Client. adark 5487 — 9y
Ad

Answer this question