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

Script to make a gui invisible or visible not working. Why is this happening?

I have made a certain key that makes a frame visible or invisible. This isn't working. Can someone tell me why and how to fix it?

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local CoolFrame = game.StarterGui.ScreenGui.Frame

Mouse.KeyDown:Connect(function(Key)
	Key = Key:lower()
	if Key == 'e' then
		if CoolFrame.Visible == false then
			CoolFrame.Visible = true 
		else
			CoolFrame.Visible = false
		end
	end
end)

1 comment

0

Keydown is deprecated.

5 answers

✓ Accepted answer

1st. Deprecated Code 2nd. You can't access GUIs from starter gui once you're playing. It's cloned to playergui.

uis = game:GetService("UserInputService")
p = game.Players.LocalPlayer
Frame = p.PlayerGui.ScreenGui.Frame
uis.InputBegan:Connect(function(k, gpe)
if not gpe and k.KeyCode == Enum.KeyCode.E then
if Frame.Visible == false then
Frame.Visible = true
else
Frame.Visible = false
end
end)

You can't acces starter gui like that.

What you can do is put the local script inside of a screen gui and then define the frame like script.Parent:WaitForChild("FrameName")

Other possibility is to have your local script whereever you have it now and access it through PlayerGui like this: player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("FrameName")

Now I won't rewrite your code for you but that's what you should consider

2 comments

0

Tried both of your ideas, neither worked.

0

you didn't edit it well

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local CoolFrame = Player.PlayerGui.ScreenGui.Frame

Mouse.KeyDown:Connect(function(Key)
    Key = Key:lower()
    if Key == 'e' then
        if CoolFrame.Visible == false then
            CoolFrame.Visible = true 
        else
            CoolFrame.Visible = false
        end
    end
end)

It's player.playergui NOT startergui, startergui contents just get copied to the playergui.

BTW~~~~do not use Mouse.KeyDown as it is deprecated, use the Context Action Service!

Three things, Mouse.KeyDown is deprecated, use the UserInputService. Also, this needs to be on the Client side in a Local Script. And finally, you are changing the StarterGui which will only take into affect after you reset and it gives you a new gui. You need to use the PlayerGui.

local UserInputService = game:GetService("UserInputService")

local Player = game.Players.LocalPlayer

local CoolFrame = Player:WaitForChild("PlayerGui").ScreenGui.Frame
 
local function onInputBegan(input,gameProcessed)
	if not gameProcessed then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			local keyPressed = input.KeyCode
			if keyPressed == Enum.KeyCode.E then
				if CoolFrame.Visible == false then
				 	CoolFrame.Visible = true 
				else
					CoolFrame.Visible = false
				end
			end
		end
	end
end

UserInputService.InputBegan:connect(onInputBegan)

3 comments

0

I am getting this error in output: 16:47:28.349 - Players.LordKing_GeorgeIII.PlayerGui.KeyDown:11: 'then' expected near '='

0

Above: on line 11, add two "="'s instead of one.

0

^ my bad

:lower() does not exist use string.lower() instead or even better use Enum

3 comments

0

neither strategy worked.

0

There are no enums for that, and :lower() does exist.

0

There are no enums for that, and :lower() does exist.

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