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.
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.