Enabling a GUI from StarterCharacterScripts

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like to enable a GUI using a local script, inside of StarterCharacterScripts.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that when I do game.ScreenGui.NightVisionGUI.Enabled = true from my local script and play the game, the GUI doesn’t actually enable.

My local script named NightVisionDetectScript inside of StarterCharacterScripts

local uis = game:GetService("UserInputService")

local debounce = false

local isActivated = false

uis.InputBegan:Connect(function(input, gameProccessingEvent)
	
	if gameProccessingEvent then return end
	
	if input.KeyCode == Enum.KeyCode.N and debounce == false and isActivated == false then
		debounce = true
		isActivated = true
		game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
		game.Lighting.Atmosphere.Density = 0
		game.Lighting.GlobalShadows = false
		game.Lighting.ColorCorrection.Enabled = false
		game.Lighting.SunRays.Enabled = false
		game.Lighting.SunRaysNVG.Enabled = true
		game.Lighting.ColorCorrectionNVG.Enabled = true
		
		for i = 1, 70 do
			wait(0.005)
			game.Lighting.ColorCorrectionNVG.Brightness = game.Lighting.ColorCorrectionNVG.Brightness - 0.01
			
		end
		
		wait(3)
		debounce = false
	elseif input.KeyCode == Enum.KeyCode.N and debounce == false and isActivated == true then
		debounce = true
		game.Lighting.OutdoorAmbient = Color3.fromRGB(141, 141, 141)
		game.Lighting.Atmosphere.Density = 0.348
		game.Lighting.GlobalShadows = true
		game.Lighting.ColorCorrectionNVG.Enabled = false
		game.Lighting.SunRaysNVG.Enabled = false
		game.Lighting.SunRays.Enabled = true
		game.Lighting.ColorCorrection.Enabled = true
		game.StarterGui.NightVisionGUI.Enabled = false
		game.Lighting.ColorCorrectionNVG.Brightness = 1
		wait(3)
		debounce = false
		isActivated = false
	end
	
end)
2 Likes

You need to enable the GUI from the Player’s player gui!

When a player spawns, the all the GUI elements from game.StarterGui are cloned into Player.PlayerGui.

Instead of game.ScreenGui.NightVisionGUI.Enabled = true, try game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui").NightVisionGUI.Enabled = true.

4 Likes

Thank you very much, not only does it work but I now know a new valuable piece of information.

4 Likes

No problem!
Might I also suggest that you define certain variables such as game:GetService("Lighting")?
It can save a lot of copy/pasting and make your code look generally tidier.

For example:

local Lighting = game:GetService("Lighting")

Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 255)
Lighting.Atmosphere.Density = 0
2 Likes