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

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

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?

1 comment

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

2 answers

✓ Accepted answer

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.

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)

1 comment

0

Sorry for the late accepted answer, thanks for the help!

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

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