Detecting player input with a script inside the player

I have made a morph that allows players to jump high when E is pressed (here I just put a print script because the issue is the input) , but that script must only work on morphed player, which is why I’m trying to make it work only when the script is in a character.

The input doesn’t seem to be detected at all. Here is the script:

local char = script.Parent
local Humanoid = char:FindFirstChild("Humanoid")
local UIS = game:GetService("UserInputService")
local player = game.Players:GetPlayerFromCharacter(char) -- Getting the Player
UIS.InputBegan:Connect(function(input, GPE) -- This will detect the input, Connecting to the input that got in, GPE = Game Processed Event and is if the game processed it and if the player is busy, like typing in the chat for an example
	if input.KeyCode == Enum.KeyCode.E then -- Making a if statement to ask if the players input is == to input
		print("works")
	end
end)

I tried putting prints on every block that defines the player that can use the input, but it seems to detect the player and character fine, with no display message. it just never detects that the player has made an input. I couldn’t find anything that solves it on the developer hub.

Have you tried instead of;

Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)

To do the following instead?

Humanoid.Jump = true

The problem I am having is not the player not jumping, it is the input not being detected. The “print” doesn’t work when e is pressed, even if the jumping was removed altogether. Also there isn’t an error message in console.

Yes, before you edited your post, that was not clear to conclude.

You could try this:

...
UIS.InputBegan:Connect(function(input, GPE)
  if GPE then return else end
  if input.KeyCode == Enum.KeyCode.Backspace then
...

This doesn’t seem to work, did I do it right?

local char = script.Parent
print(char)
local Humanoid = char:FindFirstChild("Humanoid")
print(Humanoid)
local UIS = game:GetService("UserInputService")
print(UIS)
local player = game.Players:GetPlayerFromCharacter(char) -- Getting the Player
print(player)

UIS.InputBegan:Connect(function(input, GPE)
	if GPE then return else end
	if input.KeyCode == Enum.KeyCode.Backspace then
		print("works")
	end
end)

I copied the exact block of code you sent and this is the result:

image

It works.
Make sure the LocalScript is located in Starterplayer -> StarterCharacterScripts.

image

1 Like

The issue was me using a server script instead of a local script. thank you for your help!

1 Like

You’re welcome. I suggest you dig into what makes sense to use Scripts, LocalScripts and ModuleScripts, also known as server, client and replication scripts. It’s a major part in making a game, especially once the local/client scripts meet server scripts.


Starting at the Roblox API is a good first step. Then broaden from there with online tutorials and guides.

1 Like

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