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

togglable GUI why so many parameters for yalls posts?

function PressK(key)
	if (key == "k") then
		if (Frame.Visible == true) then
			Frame.Visible = false
		elseif (Frame.Visible == false) then
			Frame.Visible = true
		end
	end
end
mouse.KeyDown:connect(PressK)

just wanted to know whats wrong with this code

4 comments

0

you are using keydown

0

Why are you wrapping your if statements in brackets?

0

@incapaz It's just a habit. Nothing wrong with it. It's actually a pretty good one. Don't quote me on this but I believe it's actually proper. I'm not totally sure tho.

0

yes in lua and python wrapping it in brackets is proper but not needed but i fixed this problem

2 answers

✓ Accepted answer

Dude you can just say Frame.Visible = not Frame.Visible instead of if's. Its way simpler trust me. Because it makes the value opposite of what it was before. Also just use UserInputService for keydown functions. They are very reliable and they work across PC and Console.

1 comment

0

And mobile.

###Mouse.KeyDown is deprecated, and so is connect. The KeyDown could be your problem.

###You should switch to ContextActionService or UserInputService to get user input. Personally, I’d prefer ContextActionService, mainly because you can bind actions to functions, make mobile exclusive buttons, and bind multiple keys to functions without if statements.

function toggleGui() --don’t include any parameters, as bind action doesn’t allow these.
    Frame.Visible = not Frame.Visible
end

game:GetService("ContextActionService"):BindAction(
    "Toggle", --name of action
    toggleGui, --function to bind
    true, --Make a mobile button
    Enum.KeyCode.K,
    Enum.KeyCode.E,
    Enum.KeyCode.F
) --you can just use one if you like, but i want 3

###That’s how to use this awesome service. That’s also how to bind multiple keys. You can use just one as well, of course.

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