How to print text when touching part and pressing key

My intention is to print a text when the player is touching a brick. I’ve used this script below:

local debounce = false
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")

script.Parent.Touched:Connect(function()
	UIS.InputBegan:Connect(function(input)
		if not debounce then
			debounce = true
			if input.KeyCode == Enum.KeyCode.E then
				print("HELP ME GOD")
			end
		end
	end)
end)

but it doesn’t seem to work. Am I supposed to use remote events here?

This should be work, you cannot add another connect function in a connect function

script.Parent.Touched:Connect(function()
		if not debounce then
			debounce = true
				print("HELP ME GOD")
		end
end)

No I mean when the player is pressing a key. Not when the player is touching the part.

Use IsKeyDown

    local UserInputService = game:GetService("UserInputService")
     
script.Parent.Touched:Connect(function()
    local theKeyE= UserInputService:IsKeyDown(Enum.KeyCode.E)
         if theKeyE then
		if not debounce then
			debounce = true
				print("HELP ME GOD")
		end
        end
end)
1 Like

I guess you’re using a local script. Local scripts won’t run in the workspace. If it’s a Server Script then UserInputService only works in local scripts.

1 Like

nope. i’m using a regular script

Also you can’t do LocalPlayer on a regular script

So the local script needs to be in replicated storage?

scripts won’t run in replicatedstorage. If you’re going to use a local script you can put it inside StarterCharacterScripts or StarterGui if you wish.

Watch here how LocalScript work

There’s 2 ways you can do this and the way you do it will be determined by what you’re trying to do.

The first method involves using the server to track players that are holding down a key. Whenever the client presses a key, fire a remote to the server that adds the player to a table to track players that are holding down the key. Fire the remote again when they release the key and remove them from said table. This however is not very performance-friendly and you may run into some issues with due time.

The second method is very similar, except you will not be firing a remote to the server. Instead you will be creating a variable (on the client) that will be used to track the state of the player. You can set this variable to true whenever a player is holding down the desired key and then check if they are touching a part. You would ideally fire a remote to the server asking the server to validate you’re standing next to the appropriate part.