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

How do I get the Onkeypress service to work?

I am trying to make a script that when a player is close enough they press "E" on their keyboard and a c4 device appears on an entry point. (The parts I change the transparency of). But when I test it, nothing happens. Any idea why?

Heres the code..

function onKeyPress(inputObject, gameProcessedEvent)
	
end

local radius = 25

for _, v in pairs(game.Players:GetPlayers()) do
	if v:DistanceFromCharacter(script.Parent:GetModelCFrame().p) < radius then
		if inputObject.KeyCode == Enum.KeyCode.E then
			script.Parent.Union.Transparency = 0
			script.Parent.Part1.Transparency = 0
			script.Parent.Part2.Transparency = 0
			script.Parent.Part3.Transparency = 0
			script.Parent.Part4.Transparency = 0
			script.Parent.Part5.Transparency = 0
			script.Parent.Part6.Transparency = 0
			script.Parent.Part7.Transparency = 0
			script.Parent.BillboardGui.Frame["Bomb Placement"].visible = false
		end
	end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

4 comments

0

Your code is outside of the onKeyPress function. Use a for loop to make all the parts in script.Parent Transparent instead.

0

I understand what your saying, don't know how to implement it. Im still very new lol

0

If you are using a "ServerScript" You need to use a "LocalScript" for use "UserInputService"

0

Ah thank you

1 answer

Well, it's likely because you haven't put anything inside of your InputBegan function.

Try moving it inside of it, like this:

(I also added a check for gameProcessedEvent. This will make sure that this doesn't trigger when the player is in a GUI window, such as the roblox Chat or Menu.)


local radius = 25

function onKeyPress(inputObject, gameProcessedEvent)
  for _, v in pairs(game.Players:GetPlayers()) do
      if v:DistanceFromCharacter(script.Parent:GetModelCFrame().p) < radius then
          if inputObject.KeyCode == Enum.KeyCode.E and not gameProcessedEvent then
              script.Parent.Union.Transparency = 0
              script.Parent.Part1.Transparency = 0
              script.Parent.Part2.Transparency = 0
              script.Parent.Part3.Transparency = 0
              script.Parent.Part4.Transparency = 0
              script.Parent.Part5.Transparency = 0
              script.Parent.Part6.Transparency = 0
              script.Parent.Part7.Transparency = 0
              script.Parent.BillboardGui.Frame["Bomb Placement"].visible = false
          end
      end
  end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

3 comments

0

Thank you

0

No problem. Be sure to mark my answer as the solution in case others have a problem similar to this.

0

I would, but it didn't work... Idk why

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