✓ Accepted answer
looking at the above answer you're almost correct. Try this:
local userInputService = game:GetService("UserInputService")
local FramePos = UDim2.new(0,0,0,0)
local YScale = 0
local Key = nil
local oldKey = nil
local NewKeyPressed = false
userInputService.InputBegan:Connect(function(input)
Key = input.KeyCode
if(Key ~= oldKey) then
NewKeyPressed = true
oldKey = Key
else
NewKeyPressed = false
end
end)
while true do
if(NewKeyPressed == true) then
if(Key == Enum.KeyCode.W) then
YScale = -0.05
else
YScale = 0
end
NewKeyPressed = false
end
FramePos = FramePos + UDim2.new(0,0,YScale,0)
print(FramePos)
wait(0.5)
end
Firstly if you have your while loop inside your InputBegin Event, as you press on the key w once all is good but as you spam w the loop will be fired multiple times causing your framePosition to move quicker and quicker each W key press.
So what I've come up with is a way to stop this by removing the loop from the event function and made it a global loop at the end of the script. Then I've made a simple memory to record the last key press via Key and oldKey whereas if they are different from it's a new key being pressed. I then recode this change and change the oldKey to the current Key value.
I've also changed the way you would move the frame, rather than changing it directly, I've made a YScale value and change that instead. So you would have better control and understanding on what we're changing.
I then fire up the loop each 0.5 of a sec and check to see if NewKeyPressed == true, if it is then we add the YScale to our frame position.
If you wanted the frame to move faster with each press of the key w then move the loop back inside the Event
Hope this helps! :)
1 comment
Thanks! After a bit of adjustion, I was able to make it work :)