Text not going back when player leaves safezone

Hi, I want to make a safezone so when the player is in it, they get infinite steps, but when they leave, they have 100. Its going to infinite but its not reverting back to 100 when they leave. Also it is glitching between “INF” and “100” even if the player is in the safezone or not. Can someone help? Thanks. Here is my current code:

local SoundRegionsWorkspace = workspace.SafeZones
local Found = false
local plr = game.Players.LocalPlayer
local steps = plr.PlayerGui.StepsGUI.Frame.TextLabel

while wait() do
	for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
		Found = false
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, plr.Character:GetDescendants())
		for _, part in pairs(parts) do
			-- Loop one by one through the parts table
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				Found = true
				break
			else
				Found = false
			end
		end
		if Found == true then
			-- Start playing some music
			if steps.Text ~= "inf" then
				steps.Text = "inf"
				break
			else steps.Text = "100"
			end
		end
	end
end

PS: I used AlvinBlox’s tutorial on soundregions which is why I have music notes in there. Forgot to take em out.

For the issue of it “glitching,” you’ve likely got multiple regions, yes? If so, you’re looping through all the regions, finding the player in one of them (which consequently sets it to INF,) and then it checks another zone and they’re not in it so it’s reverting rapidly.

Try this code:

local SoundRegionsWorkspace = workspace.SafeZones
local plr = game.Players.LocalPlayer
local steps = plr.PlayerGui.StepsGUI.Frame.TextLabel

-- task.wait() should ideally be used over wait(), and I doubt you need 60fps accuracy, 30 should be plenty for this and more performant.
while task.wait(1/30) do
	local Found = false -- define this locally, no need to bring it out of scope
	for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
		local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
		local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, plr.Character:GetDescendants())
		for _, part in pairs(parts) do
			-- Loop one by one through the parts table
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				Found = true
				break -- else statement was redundant since it's now locally declared as "false" every loop.
			end
		end
		if Found == true then
			-- Start playing some music
			break -- break the loop
		end
	end
	-- Define this outside of the loop so we're not switching between "inf" and "100" if the zone doesn't find them.
	steps.Text = if Found then "inf" else "100"
end

I believe this should work, though I have not tested it.

Sorry I took so long to respond, had to sleep then go to school. Anyways, I don’t think it would work because on the last line its setting text then an if statement but lemme try.

I have fixed it going back to 100 when they leave the safezone. Except I have different stages and each stage there is a different counter. How would I make it so that it changes back? Here is my current code but its just not changing to 450 instead its changing it to 100 which I don’t want:

local SoundRegionsWorkspace = workspace.SafeZones
local plr = game.Players.LocalPlayer
local steps = plr.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = plr.PlayerGui.StageTransfer.CurrentStage

-- task.wait() should ideally be used over wait(), and I doubt you need 60fps accuracy, 30 should be plenty for this and more performant.
function whileloop()
	while task.wait(1/30) do
		local Found = false -- define this locally, no need to bring it out of scope
		for i, v in pairs(SoundRegionsWorkspace:GetChildren()) do
			local region = Region3.new(v.Position - (v.Size/2),v.Position + (v.Size/2))
			local parts = game.Workspace:FindPartsInRegion3WithWhiteList(region, plr.Character:GetDescendants())
			for _, part in pairs(parts) do
				-- Loop one by one through the parts table
				if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
					Found = true
					break -- else statement was redundant since it's now locally declared as "false" every loop.
				end
			end
			if Found == true then
				break -- break the loop
			end
		end
		-- Define this outside of the loop so we're not switching between "inf" and "100" if the zone doesn't find them.
		steps.Text = if Found then "inf" else "0"
		if steps.Text == "0" then
			steps.Text = "100"
			break
			elseif stagetrans.Text == "1" then
			steps.Text = "100"
			break
			elseif stagetrans.Text == "2" then
			steps.Text = "125"
			break
			elseif stagetrans.Text == "3" then
			steps.Text = "125"
			break
			elseif stagetrans.Text == "4" then
			steps.Text = "25"
			break
			elseif stagetrans.Text == "5" then
			steps.Text = "200"
			break
			elseif stagetrans.Text == "6" then
			steps.Text = "250"
			break
			elseif stagetrans.Text == "7" then
			steps.Text = "110"
			break
			elseif stagetrans.Text == "8" then
			steps.Text = "75"
			break
			elseif stagetrans.Text == "9" then
			steps.Text = "75"
			break
			elseif stagetrans.Text == "10" then
			steps.Text = "75"
			break
			elseif stagetrans.Text == "11" then
			steps.Text = "125"
			break
			elseif stagetrans.Text == "12" then
			steps.Text = "450"
			break
			elseif stagetrans.Text == "13" then
			steps.Text = "200"
			break
			elseif stagetrans.Text == "14" then
			steps.Text = "150"
			break
			elseif stagetrans.Text == "15" then
			steps.Text = "200"
			break
		end
	end
end

for _,v in pairs(SoundRegionsWorkspace:GetChildren()) do
	v.Touched:Connect(function()
		whileloop()
	end)
end