My Text Label is appearing but then disappearing

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 Want my screen gui off when the player is not in a region and on if in one.

  2. What is the issue? Include screenshots / videos if possible!
    The screen gui is disappearing after a second has passed

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried putting the Gui.Enabled in other places but it doesn’t wanna work.

Here is the code

local Regions = workspace.Regions
local found = false


local gui = script.Parent:WaitForChild("EnterRegion")
local button = gui.Text



while wait(1) do
	
	for i, v in pairs(Regions:GetChildren()) do 
		gui.Enabled = false
		found = false
		local Region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local Parts = game.Workspace:FindPartsInRegion3WithWhiteList(Region,game.Players.LocalPlayer.Character:GetDescendants())
		for _,part in pairs(Parts) do
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				found = true
				gui.Enabled = true
				break
				
			else
				found = false
				gui.Enabled = false
			end
		end
		if found == true then
			if script.Parent.Songs[v.Name].IsPlaying == false then
				script.Parent.Songs[v.Name]:Play()
				button.Text = "In The "..(v:FindFirstChild("Name").Value).." Zone."
				--text = "In The "..(v:FindFirstChild("Name").Value).." Zone."
				button.BackgroundColor3 = script.Parent.Songs[v.Name].ColorInside.Value
				button.BorderColor3 = script.Parent.Songs[v.Name].ColorOutside.Value
				break
			end
		else
			script.Parent.Songs[v.Name]:Stop()
           --- i put the gui.Enabled here when trying to solve the problem but it didn't work.
			
			
		end
	end
	
end

Here is my explorer

The script is mostly from AlvinBlox since I’m new to Region3.
Give me tips on how i could improve if you want to, thanks.

1 Like

Here’s a replacement block of code that should serve your needs perfectly:

local Regions = workspace.Regions
local found, lastFound;
--
local gui = script.Parent:WaitForChild("EnterRegion")
local button = gui.Text
--
while wait(1) do
	if found then 
		lastFound = found;
	end
	--
	found = nil
	--
	for _, Region in pairs(Regions:GetChildren()) do 
		local Region = Region3.new(Region.Position - (Region.Size/2),Region.Position + (Region.Size/2))
		local Parts = game.Workspace:FindPartsInRegion3WithWhiteList(Region,{game.Players.LocalPlayer.Character})
		--
		if Parts and #Parts > 0 then
			found = Region; break
		end
	end
	--
	if found then
		gui.Enabled = true;
		--
		if script.Parent:FindFirstChild('Songs') and script.Parent.Songs:FindFirstChild(found.Name) and script.Parent.Songs[found.Name].IsPlaying == false then
			script.Parent.Songs[found.Name]:Play()
			button.Text = "In The "..(found:FindFirstChild("Name").Value).." Zone."
			button.BackgroundColor3 = script.Parent.Songs[found.Name].ColorInside.Value
			button.BorderColor3 = script.Parent.Songs[found.Name].ColorOutside.Value
		end
	else
		gui.Enabled = false;
		--
		if lastFound and script.Parent:FindFirstChild('Songs') and script.Parent.Songs:FindFirstChild(lastFound.Name) and script.Parent.Songs[lastFound.Name].IsPlaying ~= false then
			script.Parent.Songs[lastFound.Name]:Stop()
		end
		--
		lastFound = nil;
	end
end

You’re logic was just a tad condensed so every time your loop executed, it would hide the UI before it confirmed that your character was not in a region.

Best of luck!

Edit: Made some minor corrections to the else block simply due to not having an identical environment to test in :stuck_out_tongue:

2 Likes

It’s erroring with this message:

21:30:37.844 - Name is not a valid member of Region3

21:30:37.845 - Stack Begin

21:30:37.846 - Script ‘Players.GreenSubZeroGamesYT.PlayerGui.LocalScript’, Line 26

21:30:37.846 - Stack End

Btw your code is looking kinda advanced to me lol.

1 Like

Ah apologies, it’s a habit when you’ve been coding for 8+ years.

The error should alleviated by changing:

if Parts and #Parts > 0 then
	found = Region; break
end

to:

if Parts and #Parts > 0 then
	found = Parts[1]; break
end

Give it a try and let me know. I created a test environment, but I don’t have all the same instances as you so some bits may not work as equally expected.

Thanks.

2 Likes

It fixed the error in the output but now it won’t change the TextLabel properties and play the music.

1 Like

That’s because I derped, and just realized it:

Change:

local Region = Region3.new(Region.Position - (Region.Size/2),Region.Position + (Region.Size/2))
local Parts = game.Workspace:FindPartsInRegion3WithWhiteList(Region,{game.Players.LocalPlayer.Character})
--
if Parts and #Parts > 0 then
	found = Region; break
end

to:

local RegionToCheck = Region3.new(Region.Position - (Region.Size/2),Region.Position + (Region.Size/2))
local Parts = game.Workspace:FindPartsInRegion3WithWhiteList(RegionToCheck,{game.Players.LocalPlayer.Character})
--
if Parts and #Parts > 0 then
	found = Region; break
end

It should function as expected now, excuse my derp.

(It was just a matter of not overwriting the reference to the part representing the region lol)

1 Like

Thanks a ton dude! This fixed everything. Also 8 years of coding is a lot. I understand that you can make mistakes just like me. Have a nice day/night!

2 Likes

:+1: All it takes is a little practice, it builds up, and before you know it you realized you learned how to do something. Keep it up and you’ll find yourself in the same spot!

2 Likes