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

How can I make a Gui move Left and Right when you hold down a key, and then stop when you let go?

Asked by 7 months ago

So, I had a cool idea to make a 2D Roblox Game only using Guis. I've seen one YouTuber do it before, and I wanted to try and make my own version. Everything was going great until I ran into a problem.

In my game, I set both the Player's WalkSpeed and JumpPower to 0, then inserted a Frame inside of a ScreenGui in the StarterGui.

Then, I made a LocalScript inside of the Frame that controls the Frame's movement.

However, when I tested the game out and clicked the "A" and "D" keys (which are used to control the Frame to go Left and Right), it would only let me go a certain distance before stopping, even though I was still holding down the key for that direction.

What I am trying to do is to get the Frame to Copy Roblox's Actual movement system, like when you hold down the "W" key, your character moves forwards until you let go of the "W" key.

What my Frame is doing is it is stopping at a certain distance even though I am still holding down the key.

This is the Frame's Gui Movement LocalScript:

local TweenService = game:GetService("TweenService")
local Player = script.Parent.Parent
local UIS = game:GetService("UserInputService")

game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

local jumpCooldown = 0.8
local debounce = false

--//Player Movement\\--


    local PosX = Player.Position.X
    local PosY = Player.Position.Y

    if Key.KeyCode == Enum.KeyCode.W and Player.Position ~=, 0, 0.1, 0) or Key.KeyCode == Enum.KeyCode.D and Player.Position ~=, 0, 0.1, 0) then

        Player:TweenPosition( + 0.1, 0, PosY.Scale, 0), "Out", "Linear", 0.2)


    if Key.KeyCode == Enum.KeyCode.S and Player.Position ~=, 0, 0.1, 0) or Key.KeyCode == Enum.KeyCode.A and Player.Position ~=, 0, 0.9, 0) then

        Player:TweenPosition( - 0.1, 0, PosY.Scale, 0), "Out", "Linear", 0.2)


    if Key.KeyCode == Enum.KeyCode.Space and Player.Position ~=, 0, 0.6, 0) then
        if debounce == false then
            debounce = true
            Player:TweenPosition(, 0, PosY.Scale - 0.4, 0), "Out", "Sine", 0.6)
            Player:TweenPosition(, 0, PosY.Scale + 0.01, 0), "Out", "Bounce", 0.8)
            debounce = false

Any help on this?

2 answers

Log in to vote
Answered by 7 months ago

You can do this with UIS.InputBegan and UIS.InputEnded.

Yes, that could work, but I need to find a way to repeat where the Frame is going until the player stops holding the key down JackyW9cky 27 — 7mo
just do an if statement with a loop and use else 666_brithday 103 — 7mo
Log in to vote
Answered by 7 months ago

Use a variable. When UIS.InputBegan fires, the variable goes true. When UIS.InputEnded fires, the variable goes false. Then make a loop to check the variable value and move or stop that frame accordingly

Answer this question