Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I get the Onkeypress service to work?

Asked by 5 years ago
Edited 5 years ago

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)
0
Your code is outside of the onKeyPress function. Use a for loop to make all the parts in script.Parent Transparent instead. xPolarium 1388 — 5y
0
I understand what your saying, don't know how to implement it. Im still very new lol Yutube_SB101 5 — 5y
0
If you are using a "ServerScript" You need to use a "LocalScript" for use "UserInputService" yHasteeD 1819 — 5y
0
Ah thank you Yutube_SB101 5 — 5y

1 answer

Log in to vote
0
Answered by
Xiousa 156
5 years ago
Edited 5 years ago

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)
0
Thank you Yutube_SB101 5 — 5y
0
No problem. Be sure to mark my answer as the solution in case others have a problem similar to this. Xiousa 156 — 5y
0
I would, but it didn't work... Idk why Yutube_SB101 5 — 5y
Ad

Answer this question