Detect any key on keyboard pressed

I want to detect when any key is pressed on the keyboard and then fire a remote event. I have tried before and it failed.

I only want the key down to be detected if a bool value is set to true and if it is false, I want nothing to happen.

This is what I have currently
(local script)

local plrName = game.Players.LocalPlayer.Name
local char = game.Workspace:WaitForChild(plrName)
local UIS = game:GetService("UserInputService")

char:WaitForChild("IsDead").Changed:Connect(function()
	if char.IsDead.Value == true then
		-- this is where I want to detect any key press
	end
end)

I am totally stuck, I can’t find any pre existing advice on this topic.
tldr: I want to detect when any key on the keyboard is pressed.

UIS.InputBegan:connect(function(key)
	 print("Pressed: "..key)
end)

If you want to check which key it is, then check if the “key” value is a certain keycode.

edit: sorry I think I pasted the wrong script, I know this works because I’ve used it before.

I think something like this should work

local plrName = game.Players.LocalPlayer.Name
local char = game.Workspace:WaitForChild(plrName)
local UIS = game:GetService("UserInputService")
local connection

char:WaitForChild("IsDead").Changed:Connect(function()
	if char.IsDead.Value then
		connection = UIS.InputBegan:Connect(function(input,gpe)
			if gpe then return end
			--Code to happen when key is pressed
		end)
	else
		connection:Disconnect()
	end
end)

Create a connection to the InputBegan event when player is dead to listen to key presses and if they are no longer dead, disconnect the event

@Nodehayn Yes you are correct about that, but I’m unsure if @OP is using a custom character respawn handler or just using the default system, so if he’s using the default system, this will not work and another suggestion has to be made

When the player respawns, the connection wont disconnect because char.IsDead would become nil since the character has been parented to nil (destroyed) so this’ll probably just keep making connections.

It has become evident this way of doing things isn’t going to be appropriate, closed.