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

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

Asked by 6 years ago

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)

0
Keydown is deprecated. joshthegamer456 93 — 6y

5 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)
Ad
Log in to vote
0
Answered by 6 years ago

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

0
Tried both of your ideas, neither worked. ChasingNachos 133 — 6y
0
you didn't edit it well g1o2r3d4a5n6 350 — 6y
Log in to vote
0
Answered by 6 years ago
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!

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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)

0
I am getting this error in output: 16:47:28.349 - Players.LordKing_GeorgeIII.PlayerGui.KeyDown:11: 'then' expected near '=' ChasingNachos 133 — 6y
0
Above: on line 11, add two "="'s instead of one. ShutokouBattle 227 — 6y
0
^ my bad climethestair 1663 — 6y
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

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

0
neither strategy worked. ChasingNachos 133 — 6y
0
There are no enums for that, and :lower() does exist. User#19524 175 — 6y
0
There are no enums for that, and :lower() does exist. User#19524 175 — 6y

Answer this question