Why the ScreenGui is not appear?

Am trying to make if mouse.target is ‘log’ or ‘leaf’ then appear the ScreenGui, but its not be appear

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local items = {"log","leaf"}
game.StarterGui.NameChecker.Enabled = false



while true do
	local t = mouse.Target
	if t then
		for i = 1, #items do
			if items[i] == t.Name then
				print(t.Name)
				game.StarterGui.NameChecker.Enabled = true
				game.StarterGui.NameChecker.Frame.Item.Text = t.Name
			else
				game.StarterGui.NameChecker.Enabled = false
			end
		end
	end
	wait()
end

(this script is on StarterPlayerScript.LocalScript)
image

The issue here is your changing stuff inside StarterGui.

StarterGui is only a container for Guis, updating stuff here does not replicate to the Guis held in LocalPlayer.PlayerGui, where you should be updating your Guis from, if you want to see updates live.

game.StarterGui.NameChecker becomes LocalPlayer.PlayerGui.NameChecker

Dont worry, its a really common mistake, you’re not the first to run into it, and definitely not the last.

2 Likes
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local items = {"log","leaf"}
plr.PlayerGui.NameChecker.Enabled.PlayerGui = false



while true do
	local t = mouse.Target
	if t then
		for i = 1, #items do
			if items[i] == t.Name then
				print(t.Name)
				plr.PlayerGui.NameCheckerr.Enabled = true
				plr.NameChecker.Frame.Item.Text = t.Name
			else
				plr.PlayerGui.NameChecker.Enabled = false
			end
		end
	end
	wait()
end

I tried but it makes me error
NameChecker is not a valid member of PlayerGui “Players.MoonScro.PlayerGui”
whats problem?
image

Use WaitForChild, then it makes sure it exists first before running all the code. Just do it once at start of the script, no need to do it for every line.

plr.PlayerGui:WaitForChild(‘NameChecker’)

Also please use Stepped or Heartbeat for something like this. You also don’t need to use wait here. If you need a wait use task.wait() instead. Which without number or 0 is equivalent to RunService.Heartbeat:Wait(), it runs once every frame, which Stepped also does already by itself.

local RunService = game:GetService('RunService')

RunService.Stepped:Connect(function()
	--loop code
end)
1 Like

There are a few problems I see:

  1. As other forum users have said, your script only edits the copy of the GUI in StarterGui. You’ll want to edit the version in PlayerGui. To do this, I’d recommend moving your code to a new local script inside the NameChecker ScreenGui and reference the ScreenGui with script.Parent.

  2. You need to break your loop. Currently, your loop checks if the name is items[i], then checks again and again. The problem is, if it find something with a good name it then checks the next name and resets what was done when it found a good name. Example: name equals “log” show the Gui, next iteration name (which is “log”) doesn’t equal “leaf”, hide gui again.

    I explained that somewhat poorly, but all you need to do to fix that is add the break keyword. This makes it so if it finds a valid name it stops comparing it to the other names:

for i = 1, #items do
			if items[i] == t.Name then
				print(t.Name)
				game.StarterGui.NameChecker.Enabled = true
				game.StarterGui.NameChecker.Frame.Item.Text = t.Name
				break -- Found a good name! Exiting loop so next iteration doesn't un-enable gui. 
			else
				game.StarterGui.NameChecker.Enabled = false
			end
		end

Just a side note, the table.find function might simplify your code. I’d also recommend using RenderStepped, so your code updates every frame instead of every wait() (also would recommend task.wait() instead of wait(), it’s a newer version that works better).

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local items = {"log","leaf"}
-- TODO: Change reference
-- Recommendation: Make a variable named something like screenGui and set it to NameChecker
game.StarterGui.NameChecker.Enabled = false


game:GetService("RunService").RenderStepped:Connect(function()
	local t = mouse.Target
	-- If target and "items" table has target name
	if t and table.find(items, t.Name) then
		-- TODO: change script location and this reference (other three as well)
		game.StarterGui.NameChecker.Enabled = true
		game.StarterGui.NameChecker.Frame.Item.Text = t.Name
	else
		game.StarterGui.NameChecker.Enabled = false
	end
end)
1 Like