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