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

Why doesn't the frame open when using onKeyPress function?

Asked by
Jxemes 75
5 years ago

The goal of this script is whenever a player presses G, a frame should be visible. However, it doesn't and I can't seem to find the issue. Here's the code:

``` local frame = script.Parent:WaitForChild("Frame") local open = script.Parent.Open

function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.G then if open.Value == false then frame.Visible = true open.Value = true elseif open.Value == true then frame.Visible = false open.Value = false end end end

game:GetService("UserInputService").InputBegan:connect(onKeyPress) ``` Anyone found the problem?

0
just do if frame.Visibe == true then frame.Visible = false i guess or you can try and make UserInputService a variable. or do :Connect since i think :connect is depricated WillBe_Stoped 71 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Hey, Jxemes!

Pretty close solution here. There are a few things we can do to make this code a little bit cleaner! The most notable change is that frame.Visible is a boolean value. So, we can use the "not" operator to check if frame.Visible is a "falsey" value, or anything that pretty much equals false. This eliminates two extra if statements!

It'll basically just say, whatever frame.Visible is, true or false, make it the opposite.

Edit: Had a lot of random double spaces.

```lua local UIS = game:GetService('UserInputService'); local frame = script.Parent:WaitForChild('Frame');

UIS.InputBegan:Connect(function(inputObject, gameProcessedEvent) if inputObject.UserInputType == Enum.UserInputType.Keyboard then if inputObject.KeyCode == Enum.KeyCode.G then frame.Visible = not frame.Visible; end end end) ```

0
Sorry for the late accepted answer, thanks for the help! Jxemes 75 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
local frame = script.Parent:WaitForChild("Frame")
local UIS = game:GetService("UserInputService") -- Gets the service for input

UIS.InputBegan:Connect(function(InputObject,gameProcessedEvent) -- When input begins then
if InputObject.KeyCode = Enum.KeyCode.G then 

if frame.Visible == false then -- If the frame is closed then
frame.Visible = true  -- Open it.

elseif frame.Visible == true then -- If the frame is open then
frame.Visible = false -- Close it.
end
end
end)


--Hope this helps

Answer this question