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)
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
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)
:lower() does not exist use string.lower() instead or even better use Enum