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

GUI KeyUp Visible Event Not Firing Correctly? (Should Be an Easy debug)

Alright, as of now, I've pretty much got two variants of a script, both with strange worthwhile events to note. The script is supposed to do as follows:

  • When 'e' (101) is pressed, it activates the function.
  • If a boolean is set to true, it makes a GUI's visible trait false and sets the boolean mentioned first to false.
  • If the boolean is set to false, it makes a GUI's visible trait true and sets the boolean to true.

Here's the first variant:

local Gvis1 = game.Workspace.Tycoon.Tycoon.GuVis1 -- GuVis1 is a boolean value.
local mouse = game.Players.LocalPlayer:GetMouse()

function onKeyUp( key )
if(string.char( 101 ) == key) then

if Gvis1.Value == true then

game.Players.LocalPlayer.PlayerGui.ScreenGui.BackFrame.Visible = false
Gvis1 = false	

end

if Gvis1.Value == false then

game.Players.LocalPlayer.PlayerGui.ScreenGui.BackFrame.Visible = true
Gvis1 = true

end

else
print("Key:", key, " Code:", string.byte(key))
end
end
mouse.KeyUp:connect(onKeyUp)

This variant is kind of strange. It has a notable difference in that when referencing the Gvis1 = false (or true) it doesn't say ".Value". This one does the following:

  • When any letter that isn't "e" is pressed, the alternative to pressing "e" fires correctly.
  • Whenever I press "e" (the first time) the GUI goes invisible (it's visible by default)
  • If I press "e" a second time, the script does nothing, and gives the following error:

Players.Player1.PlayerGui.ScreenGui.GuiTyc1(The script name):11: Attempt to index upvalue 'Gvis1' (a boolean value)

  • The function holding this then breaks.

However, if I do place a .Value after Gvis1 = true (and false), then all it does is this:

  • Absolutely nothing. I receive no output, and nothing occurs with the Gui.

In case it couldn't be seen before, this script (which is a localscript) resides inside the Gui being changed while in the players PlayerGui.

Thanks for reading, and any help is greatly appreciated!

1 answer

✓ Accepted answer

The script gives you that error because you're setting Gvis1 itself to true, instead of it's Value. That's why it's erroring. Here's the fixed code:

local Gvis1 = game.Workspace.Tycoon.Tycoon.GuVis1 -- GuVis1 is a boolean value.
local mouse = game.Players.LocalPlayer:GetMouse()

function onKeyUp( key )

	if key:lower() == "e" then --Simpler to check if the key is "e" instead of using a bytecode

		if Gvis1.Value == true then

			game.Players.LocalPlayer.PlayerGui.ScreenGui.BackFrame.Visible = false
			Gvis1.Value = false --Set the Value to true, not the variable "Gvis"

		else

			game.Players.LocalPlayer.PlayerGui.ScreenGui.BackFrame.Visible = true
			Gvis1.Value = true

		end

	end

end

mouse.KeyUp:connect(onKeyUp)

1 comment

0

Hmm, that's strange, the actual meat of the script (in my second variant) is very similar to that. But, regardless, yours is functioning just fine, and like usual, I've learned a few very useful things! Thanks for the helpful reply.

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