Can't detect IsKeyDown when it's InputEnded

What do you want to achieve? Keep it simple and clear!
I want to make a Tween when player hits both W and D keys and turns back into the same position when stop pressing the keys. (InputBegan working but InputEnded isn’t working.)

What is the issue? Include screenshots / videos if possible!
It doesn’t detect anything when I stop pressing W and D keys. (not prints the text too.)

local userInputService = game:GetService("UserInputService") 

userInputService.InputBegan:Connect(function(input, gpe) --Working
	if gpe then return end
	if userInputService:IsKeyDown(Enum.KeyCode.W) and userInputService:IsKeyDown(Enum.KeyCode.D) then
		Angle = -25 
		Tween0C0()
		TweenC0()
		Tween1C0()
		Tween2C0()
		script.sound:Play()
		print("DW 1")
	end
end)

userInputService.InputEnded:Connect(function(input, gpe)  --Not working
	if gpe then return end
	if userInputService:IsKeyDown(Enum.KeyCode.W) and userInputService:IsKeyDown(Enum.KeyCode.D) then
		Angle = 0
		Tween0C0()
		TweenC0()
		Tween1C0()
		Tween2C0()
		script.sound:Play()
		print("DW 0")
	end
end)

That’s because when you stop pressing w, well the key isn’t down anymore so the IsKeyDown will return false and it won’t run your code. It will run if you press an extra key that isn’t W or D while you’re holding W and D, but I don’t think that is what you want. Your code doesn’t make sense, so you want to check if input is W then also check if D is down (inside the inputended connection). If input is D then check if W is also down and then run your tweens.

1 Like

Instead I suggest to manually detect it.

  1. Have variables for both keys, set them true/false in their respective functions
  2. Call a function which checks if both are true + runs the code, we will call this the update function.
1 Like
local userInputService = game:GetService("UserInputService") 

userInputService.InputBegan:Connect(function(input, gpe) --Working
	if gpe then return end
	if userInputService:IsKeyDown(Enum.KeyCode.W) and userInputService:IsKeyDown(Enum.KeyCode.D) then
		Angle = -25 
		Tween0C0()
		TweenC0()
		Tween1C0()
		Tween2C0()
		script.sound:Play()
		print("DW 1")
	end
end)

userInputService.InputEnded:Connect(function(input, gpe)  --Not working
	if gpe then return end
	if (userInputService:IsKeyDown(Enum.KeyCode.W) and input.KeyCode == Enum.KeyCode.D) or (userInputService:IsKeyDown(Enum.KeyCode.D) and input.KeyCode == Enum.KeyCode.W) then
		Angle = 0
		Tween0C0()
		TweenC0()
		Tween1C0()
		Tween2C0()
		script.sound:Play()
		print("DW 0")
	end
end)
1 Like

Working right now, thanks. Just took a break for 1,5 year from any coding and make dumb mistakes lol.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.