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

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 1 year 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\\--

UIS.InputBegan:Connect(function(Key)

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

    if Key.KeyCode == Enum.KeyCode.W and Player.Position ~= UDim2.new(PosX.Scale, 0, 0.1, 0) or Key.KeyCode == Enum.KeyCode.D and Player.Position ~= UDim2.new(PosX.Scale, 0, 0.1, 0) then

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

    end

    if Key.KeyCode == Enum.KeyCode.S and Player.Position ~= UDim2.new(PosX.Scale, 0, 0.1, 0) or Key.KeyCode == Enum.KeyCode.A and Player.Position ~= UDim2.new(PosX.Scale, 0, 0.9, 0) then

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

    end

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

Any help on this?

2 answers

Log in to vote
3
Answered by 1 year ago

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

0
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 — 1y
0
just do an if statement with a loop and use else 666_brithday 103 — 1y
Ad
Log in to vote
1
Answered by 1 year 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