How to create a hold and release keybind using contextactionservice

I have been trying to create a hold and release keybind that uses contextactionservice. I only get a result from both the client and server once the key is held down, as when the key is released, the client and server don’t print any result back.

--Client--
local function ImpactFistEMove(Name,State,Object)
		if Name == "HoldAndRelease" and State == Enum.UserInputState.Begin then
			if UIS:IsKeyDown(Enum.KeyCode.E) == true then
					local HoldDownKey = UIS:IsKeyDown(Enum.KeyCode.E)
					print("E Key held down")
					ImpactRemote:FireServer(HoldDownKey)
		if Name == "HoldAndRelease" and State == Enum.UserInputState.End then
			if UIS:IsKeyDown(Enum.Keycode.E) == false then
					print("E Key released")
					HoldDownKey = UIS:IsKeyDown(Enum.KeyCode.E)
					ImpactRemote:FireServer(HoldDownKey)
				end
			end
		end
	end
end
--Server--
ImpactRemote.OnServerEvent:Connect(function(Player,HoldDownKey)
	local Character = Player.Character
	if HoldDownKey == true then
			print(HoldDownKey)
	if HoldDownKey == false then
			print(HoldDownKey)
		end
	end
end)

I’m trying to find out how to get the server to trigger a response whenever the ‘E’ key is released from the client

2 Likes

The issue is that when you created another statement whether the button/input is released, you added another if statement instead of an elseif statement

Add this line in your input release line:

elseif Name == "HoldAndRelease" and State == Enum.UserInputState.End then

1 Like

This ended up being the main problem, thank you! I also changed the code so that it now works on both the client and server.

--Client--
local function ImpactFistEMove(Name,State,Object)
	if Name == "HoldAndRelease" and State == Enum.UserInputState.Begin then
		local HoldDownKey = true
		print("E Key held down")
		ImpactRemote:FireServer(HoldDownKey)
	elseif Name == "HoldAndRelease" and State == Enum.UserInputState.End then
		local HoldDownKey = false
		print("E Key released")
		ImpactRemote:FireServer(HoldDownKey)
	end
end
--Server--
ImpactRemote.OnServerEvent:Connect(function(Player,HoldDownKey)
	local Character = Player.Character
	if HoldDownKey == true then
		print(HoldDownKey)
	elseif HoldDownKey == false then
		print(HoldDownKey)
	end
end)
1 Like

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