My script that I have created makes a GUI visible when I press the key "R". However when I run the script and press R the GUI says that it is visible in the properties, however I still cannot see the GUI. I think the problem lies inside the settings of the GUI but I haven't found any errors.
Side note: The script is local and is stored in the Starterpack.
Here is a picture of my GUI that I want to open: https://gyazo.com/bda261e322fcf93ed675369de887bc80
Here is my code:
repStorage = game:GetService("ReplicatedStorage") BuildRoad = repStorage:WaitForChild("BuildRoad") BuildStructure = repStorage:WaitForChild("BuildStructure") while game.Players.LocalPlayer == nil do wait() end plr = game.Players.LocalPlayer mouse = plr:GetMouse() local RoadBuilding = script.Parent.Parent.StarterGui.ScreenGui.RoadBuilding mouse.KeyDown:Connect(function(key) if key == "r" then print("pressing r") if game.StarterGui.ScreenGui.RoadBuilding.Visible == false then print("closed to open") game.StarterGui.ScreenGui.RoadBuilding.Visible = true else print("open to closed") game.StarterGui.ScreenGui.RoadBuilding.Visible = false end end end)
You made a very common mistake. StarterGui
is used to store templates of guis which are later cloned to the PlayerGui
, which you should use instead.
Also, that while loop is pointless, so you can get rid of it, and you should make your variables local unless there is really a need to make them global.
local repStorage = game:GetService("ReplicatedStorage") local BuildRoad = repStorage:WaitForChild("BuildRoad") local BuildStructure = repStorage:WaitForChild("BuildStructure") local plr = game.Players.LocalPlayer local mouse = plr:GetMouse() local RoadBuilding = script.Parent.Parent.StarterGui.ScreenGui.RoadBuilding mouse.KeyDown:Connect(function(key) if key == "r" then print("pressing r") if plr.PlayerGui.ScreenGui.RoadBuilding.Visible == false then print("closed to open") plr.PlayerGui.ScreenGui.RoadBuilding.Visible = true else print("open to closed") plr.PlayerGui.ScreenGui.RoadBuilding.Visible = false end end end)
There are too many wrongs in your code. First of all, mouse.KeyDown is deprecated. Do not use it for new work. Use UserInputService
Second, your GUI's location isn't StarterGui. If you make StarterGui's GUI visible it won't turn visible on of your script. You should use Player.PlayerGui.ScreenGui.RoadBuilding.Visible = true
Third, this is not a wrong in your script but you can reach Player by using local Player = game.Players.LocalPlayer
but for that your script MUST be LocalScript.
Fourth, and yeah as I said at Third, use LocalScript. That's what you should use in GUIs.
I suggest you to re-type your script from the start. It's not long and re-typing it would be healthier for you.