Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Moving a player until the key is released?

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

✓ Accepted answer

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.

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

1 comment

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.

Mouse.KeyDown:connect(function(Key)
	if Key== "a" then 
		moveLeft()
	until Key ~= "a"
	end

Try that?

1 comment

0

It does not work, also you really shouldn't answer a question unless you have a good answer...

This discussion is preserved as it was. New answers, comments, and votes are closed.