Detect when a player press key

Hello,

I want see when a player press a key, so I decided to make this:

Server:
attack:FireClient(player, HumanoidRootPart)

Client:

local uis = game:GetService("UserInputService")
local rp = game:GetService("ReplicatedStorage")
local spaceHeld = uis:IsKeyDown(Enum.KeyCode.E)

local attack = rp:WaitForChild("attack")

uis.InputBegan:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.F then
		attack:FireServer()
	end	

end)

attack.OnClientEvent:Connect(function(HumanoidRootPart)
	local function inputBegan(object,gameProcessedEvent)
		if HumanoidRootPart.IsKeyDown == spaceHeld then
				print("Player pressed space!!1!")
			end
	end
	uis.InputBegan:Connect(inputBegan)
end)

I coudnt figure out whats the problem.

it just tell me:
IsKeyDown is not a valid member of Part “Workspace.Iwantbebetter.HumanoidRootPart”

So for the FireClient (Server for Fire) I do have;

player.Character
player
Humanoid
HumanoidRootPart

and everything what I have been tested didnt work.

I think there is more than KeyCode because with KeyCode it didnt work too. I tried everything out ,and yea nothing worked. Can Somoene help me pls?

6 Likes

why, this is a different category or am I wrong?

why should he delete it, and why do you tell me you can decide that

Here, this should work :smiley:

local uis = game:GetService("UserInputService")
local rp = game:GetService("ReplicatedStorage")

local attack = rp:WaitForChild("attack")

uis.InputBegan:Connect(function(input)
	if (uis:GetFocusedTextBox()) then
		return; -- make sure player's not chatting!
	end
	if input.KeyCode == Enum.KeyCode.F then
		attack:FireServer()
	end	

end)

attack.OnClientEvent:Connect(function(HumanoidRootPart)
	local OneTimeConnection = nil
	local function inputBegan(object,gameProcessedEvent) 
		if (uis:GetFocusedTextBox()) then
			return; -- make sure player's not chatting!
		end
		if (object.KeyCode == Enum.KeyCode.Space) then
                       print("Player pressed space!")
			OneTimeConnection = OneTimeConnection and OneTimeConnection:Disconnect() or nil
		end
	end
	OneTimeConnection = uis.InputBegan:Connect(inputBegan)
end)
22 Likes

You write make sure player is not chatting. Should I write anything there or is that all with GetFocusedTextBox

1 Like

It’s all with GetFocusedTextBox.

1 Like

InputBegan gives 2 params, first being input, second being boolean stating whether game processed that input already. If player is typing, it should be true. You can check it like so:

uis.InputBegan:Connect(function(inputObject: InputObject, gameProcessed:boolean)
    if gameProcessed then return end -- If game processed, return instantly
    --> Logic goes here...
end)
6 Likes