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

Moving a player until the key is released?

Asked by
Bman8765 270 Moderation Voter
9 years ago

Okay so I have a Localscript found in the player's screengui in startergui. It's basic function is simple. Move a image frame when you press a letter. Here is my problem, when the user holds down a key I want the player to keep moving until they let go of the key, how exactly would I do this?

The code:

repeat wait() until game.Players.LocalPlayer.PlayerGui
----------------------------------------------------------
-- Disable roblox's dumb stuff
---------------------------------------------------------- 
game.StarterGui:SetCoreGuiEnabled("All", false)
----------------------------------------------------------
-- Global Variables
---------------------------------------------------------- 
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local sprite = script.Parent.Player
----------------------------------------------------------
-- Movement
----------------------------------------------------------
function moveLeft()
    sprite.Position = sprite.Position - UDim2.new(0.01,0,0,0)
end 
function moveRight()
    sprite.Position = sprite.Position + UDim2.new(0.01,0,0,0)
end 
----------------------------------------------------------
-- Keyboard input
---------------------------------------------------------- 
Mouse.KeyDown:connect(function(Key)
    if Key == "a" then
        moveLeft()
    end
    if Key == "d" then
        moveRight()
    end
end)

3 answers

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Here's how I would write this code:

repeat wait() until game.Players.LocalPlayer.PlayerGui
----------------------------------------------------------
-- Disable roblox's dumb stuff
---------------------------------------------------------- 
game.StarterGui:SetCoreGuiEnabled("All", false)
----------------------------------------------------------
-- Global Variables
---------------------------------------------------------- 
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local sprite = script.Parent.Player

local keysDown = {}
----------------------------------------------------------
-- Movement
----------------------------------------------------------
game:GetService("RunService").RenderStepped:connect(function()
    local  move = 0
    if keysDown.a then move = move - 1 end
    if keysDown.d then move = move + 1 end  
    sprite.Position = sprite.Position + UDim2.new(0.01*move,0,0,0)
end)
----------------------------------------------------------
-- Keyboard input
---------------------------------------------------------- 
Mouse.KeyDown:connect(function(Key)
    keysDown[Key] = true
end)
Mouse.KeyUp:connect(function(Key)
    keysDown[Key] = false
end)

This is a variant of what Perci1 suggested you do.

Ad
Log in to vote
2
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

I'd use a 'while' loop and some static boolean values to make sure that it's doing what it should be doing at a given time.

isGoingLeft = false
isGoingRight = false

Mouse.KeyDown:connect(function (Key)
    if Key:lower() == "a" then
        isGoingLeft = true
        while isGoingLeft do
            if isGoingLeft == false then
                break
            end
            moveLeft()
            if isGoingLeft == true then
                wait(.25)
            elseif isGoingLeft == false then
                break
            end
        end
    end
    if Key:lower() == "d" then
        isGoingRight = true
        while isGoingRight do
            if isGoingRight == false then
                break
            end
            moveRight()
            if isGoingRight == true then
                wait(.25)
            elseif isGoingRight == false then
                break
            end
        end
    end
end

Mouse.KeyUp:connect(Key)
    if Key:lower() == "a" then
        isGoingLeft = false
    end
    if Key:lower() == "d" then
        isGoingRight = false
    end
0
An easier way to do it (in my opinion) would be to simply place a bool value in the player, then control that with the KeyDown events. Now a while loop can be used to continuously check if that value is true or false. Perci1 4988 — 9y
Log in to vote
-2
Answered by
zub74 0
9 years ago
Mouse.KeyDown:connect(function(Key)
    if Key== "a" then 
        moveLeft()
    until Key ~= "a"
    end

Try that?

0
It does not work, also you really shouldn't answer a question unless you have a good answer... Bman8765 270 — 9y

Answer this question