Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why dose this not respond when its pressed (script)?

i have this it gives me no errors but it dosnt do what its supposed to

local UserInputService = game:GetService("UserInputService")

while wait(0.1) do
	if script.Parent.Visible == true then
		function onKeyPress(inputObject, gameProcessedEvent)
			if inputObject.KeyCode == Enum.KeyCode.E then
				print("R was Pressed!")
			end
		end
	end
end

UserInputService.InputBegan:Connect(onKeyPress)

1 comment

0

It has to be Local Script to detect UserInputService signal and Local Script has to be in player locations like StarterPack, StarterGui, StarterCharacterScripts or StarterPlayerScripts. Obviously it will print "R was Pressed!" on local client, so to detect it on server you would have to use Remote Events.

2 answers

There are a few logistical errors with your code

Put the billboard in Starter GUI, Set the adornee to the part, and use a local script.

Here is my revised version:

local UserInputService = game:GetService("UserInputService")

function onKeyPress(inputObject, gameProcessedEvent)
	if script.Parent.Visible == true then
        	if inputObject.KeyCode == Enum.KeyCode.E then
                	print("R was Pressed!")
            	end
	end
end


UserInputService.InputBegan:Connect(onKeyPress)

2 comments

0

UserInputService is not a valid global

0

Make sure you have it defined like i have it written. I added it to my answer as well.

Your first problem is you're using a server script IT IS IMPOSSIBLE TO USE USERINPUTSERVICE IN A SERVER SCRIPT.

Secondly, you are using an infinite loop with no end, which you don't need in the first place!

Here's your new code:

-- local script
local UserInputService = game:GetService("UserInputService")
function onKeyPress(inputObject, gameProcessedEvent)
     if script.Parent.Visible == true then
        if inputObject.KeyCode == Enum.KeyCode.E then
            print("R was Pressed!")
        end
    end
end

UserInputService.InputBegan:Connect(onKeyPress)

This discussion is preserved as it was. New answers, comments, and votes are closed.