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
10 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:

01repeat wait() until game.Players.LocalPlayer.PlayerGui
02----------------------------------------------------------
03-- Disable roblox's dumb stuff
04----------------------------------------------------------
05game.StarterGui:SetCoreGuiEnabled("All", false)
06----------------------------------------------------------
07-- Global Variables
08----------------------------------------------------------
09local Player = game.Players.LocalPlayer
10local Mouse = Player:GetMouse()
11local sprite = script.Parent.Player
12----------------------------------------------------------
13-- Movement
14----------------------------------------------------------
15function moveLeft()
View all 31 lines...

3 answers

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

Here's how I would write this code:

01repeat wait() until game.Players.LocalPlayer.PlayerGui
02----------------------------------------------------------
03-- Disable roblox's dumb stuff
04----------------------------------------------------------
05game.StarterGui:SetCoreGuiEnabled("All", false)
06----------------------------------------------------------
07-- Global Variables
08----------------------------------------------------------
09local Player = game.Players.LocalPlayer
10local Mouse = Player:GetMouse()
11local sprite = script.Parent.Player
12 
13local keysDown = {}
14----------------------------------------------------------
15-- Movement
View all 31 lines...

This is a variant of what Perci1 suggested you do.

Ad
Log in to vote
2
Answered by
Redbullusa 1580 Moderation Voter
10 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.

01isGoingLeft = false
02isGoingRight = false
03 
04Mouse.KeyDown:connect(function (Key)
05    if Key:lower() == "a" then
06        isGoingLeft = true
07        while isGoingLeft do
08            if isGoingLeft == false then
09                break
10            end
11            moveLeft()
12            if isGoingLeft == true then
13                wait(.25)
14            elseif isGoingLeft == false then
15                break
View all 41 lines...
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 — 10y
Log in to vote
-2
Answered by
zub74 0
10 years ago
1Mouse.KeyDown:connect(function(Key)
2    if Key== "a" then
3        moveLeft()
4    until Key ~= "a"
5    end

Try that?

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

Answer this question