ScreenGUI not being enabled

Hey! So i’m trying to make this screenGUI become enabled when the user presses E (yea thats it). But for some reason it isn’t working, and it is a local script. Theres also no errors, warnings, or anything. I also did prints and they detected the keycode but not do the screengui enabling

local UIS = game:GetService("UserInputService")
local ScreenGUI = script.Parent
local plr = game.Players.LocalPlayer
local tool = plr.Backpack.Hammer
local char = game.Workspace[tostring(plr)]

UIS.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed then
		if input.KeyCode == Enum.KeyCode.E then
			if char:FindFirstChild("Hammer") then
				if ScreenGUI.Enabled == false then
					ScreenGUI.Enabled = true
				end
				if ScreenGUI.Enabled == true then
					ScreenGUI.Enabled = false
				end
			end
		end
	end
end)

Although, you could use elseif to join both declarations and this would solve your problem, you can also use ScreenGUI.Enabled = not ScreenGUI.Enabled to invert the property

local UIS = game:GetService("UserInputService")
local ScreenGUI = script.Parent
local plr = game:GetService("Players").LocalPlayer
local tool = plr.Backpack.Hammer

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
		if plr.Character and plr.Character:FindFirstChild("Hammer") then
			ScreenGUI.Enabled = not ScreenGUI.Enabled
		end
	end
end)
1 Like

thanks! and thanks again for the memory saving, im still new to scripting and saving memory

1 Like
local UIS = game:GetService("UserInputService")
local ScreenGUI = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local tool = plr:WaitForChild("Backpack"):WaitForChild("Hammer")

UIS.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then
		return
	end
	tool.Equipped:Connect(function()
		if input.KeyCode == Enum.KeyCode.E then
			if not ScreenGUI.Enabled then
				ScreenGUI.Enabled = true
			elseif ScreenGUI.Enabled then
				ScreenGUI.Enabled = false
			end
		end
	end)
end)

You can check if a tool is equipped by using the “Equipped” event which is fired whenever a tool is equipped instead of checking if the tool is inside the character of the player as opposed to inside of the backpack of the player. I’ve also made some slight optimisations & added waits to allow for instances to load before attempting to use them within the script.