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

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

Asked by
RoboFrog 400 Moderation Voter
10 years ago

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

Log in to vote
0
Answered by 10 years ago

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)
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. RoboFrog 400 — 10y
Ad

Answer this question