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