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

How do i make this script so my inventory goes away the next time you press e?

THE OPENING PART IS WORKING Here is the script

--Ignore lines 1 to 9 thats the working part
--Opening part
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(key)
    if key:lower() == "e" then
        plr.PlayerGui.Inventory.Main.Visible = true
    end
end)

--Closing part
mouse.KeyDown:connect(function(key)
	if plr.PlayerGui.Inventory.Main.Visible == true then
	if key:lower() == "e" then
		plr.PlayerGui.Inventory.Main.Visible = false
	   end	
	end
end)

No output appears

2 answers

✓ Accepted answer
mouse.KeyDown:connect(function(key)
    if key:lower() == "e" then
        plr.PlayerGui.Inventory.Main.Visible = true
else
        plr.PlayerGui.Inventory.Main.Visible = false
    end
end)

You can also simplify your script by using the "not" keyword.

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

mouse.KeyDown:connect(function(key)
    if key:lower() == "e" then
        plr:WaitForChild("PlayerGui").Inventory.Main.Visible = not plr:WaitForChild("PlayerGui").Inventory.Main.Visible --Sets the visibility of the GUI object to the opposite of what it is already, so false is true and true is false.
    end
end)

Gives you less lines in the script, and saves repeating the KeyDown event twice.

3 comments

0

Not working :(

0

Edited, should work. Make sure the code is in a Local Script. If it still doesn't work, try placing the local script in the StarterPack, that worked for me.

0

I just decided to make a close button

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