Still running function while key is not held down?

The problem is that it doesn’t detect InputEnded? I’ve got no clue on how to solve this.

local Keys = {["W"] = Enum.KeyCode.W, ["A"] = Enum.KeyCode.A, ["S"] = Enum.KeyCode.S, ["D"] = Enum.KeyCode.D}
local Active = false
local CurrentKey

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	for a,b in pairs(Keys) do
		if UserInputService:IsKeyDown(b) then
			print("[CLIENT] - Started input!")
			CurrentKey = a
			Active = true
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	for a,b in pairs(Keys) do
		if UserInputService:IsKeyDown(b) then
			print("[CLIENT] - Ended input!")
			Active = false
		end
	end
end)

RunService.RenderStepped:Connect(function()
	if Active then -- run below active.
		Battle:MoveDirection(Client.BattleFrame.Area.Player, CurrentKey)
		Battle:Boundary(Client.BattleFrame.Area.Player, Client.BattleFrame.Area)
	end
end)
1 Like

I think it might be because you’re checking if the key is still being pressed using :iskeydown in the function that activates when the key stops being pressed? I’m not super familiar with UserInputService but I think this might be it.

1 Like

I think I solved this problem.

UserInputService.InputEnded:Connect(function(input, gameProcessed)
for a,b in pairs(Keys) do
	if UserInputService:IsKeyDown(b) then
		print("[CLIENT] - Ended input!")
		Active = false
	end
end
end)

In this code, you want to find which key is released, but :IsKeyDown() Only works when you pressed key in keyboard.

So, I fixed like this using CurrentKey value:

local UserInputService = game:GetService("UserInputService")

local Keys = {["W"] = Enum.KeyCode.W, ["A"] = Enum.KeyCode.A, ["S"] = Enum.KeyCode.S, ["D"] = Enum.KeyCode.D}
local Active = false
local CurrentKey

UserInputService.InputBegan:Connect(function(input, gameProcessed)
for a,b in pairs(Keys) do
	if UserInputService:IsKeyDown(b) then
		print("[CLIENT] - Started input!")
		CurrentKey = b
		Active = true
	end
end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessed)
	if input.KeyCode == CurrentKey and input.UserInputType == Enum.UserInputType.Keyboard then
		print("[CLIENT] - Ended input!")
		Active = false
	end
end)

I tested and this code works fine, I’ll hope this code works for you.

1 Like

Works great but if I hold “W” or “S” and then proceed to click “A” or “D” while “W” or “S” is held, it ignores “A” or “D”, which is critical.

image

From InputEnded, you’re using IsKeyDown. A key wouldn’t be down if the player lifts their finger off it. It’d be more logical to use that nice dictionary format you already have, as well as how enums are nicely set up. You could even remove the loops.

local Keys = {["W"] = Enum.KeyCode.W, ["A"] = Enum.KeyCode.A, ["S"] = Enum.KeyCode.S, ["D"] = Enum.KeyCode.D}
local Active = false
local CurrentKey

UserInputService.InputBegan:Connect(function (input, gameProcessedEvent)
    if Keys[input.KeyCode.Name] then
        CurrentKey = input.KeyCode.Name
        Active = true
    end
end)

UserInputService.InputEnded:Connect(function (input, gameProcessedEvent)
    if Keys[input.KeyCode.Name] then
        Active = false
    end
end)

This in itself is a pretty rudimentary script though. If you needed multi-directional input, you’d be at a bit of a loss. Usually for a situation like this, what I’d do is have passed input modify the X and Z values of a Vector3 value and set an active flag to true if at least one key was active, then in the movement section I would pass the Vector3 to MoveDirection.

By the way, use Heartbeat not RenderStepped because you aren’t updating the character transparency nor the camera. RenderStepped should scarcely be present in production code, there aren’t too many cases where you need it but enough cases not to make it completely useless.

1 Like

It’s a GUI (2D) movement system. You could look into it if you want to. I have no clue on how I can make it support multiple key inputs after eachother without stopping/not detecting.

Movement.rbxm (4.9 KB)