How to detect if player in safezone?

Hi, I want to make a sort of safe zone/lobby type thing and I want to detect when a player is inside that safe zone, their steps become INF. I’ve got it working except it doesn’t revert when they step out. I’ve seen on the DevForum ways to do this but none of them work for me. Can someone help? Thanks.

Here is my current code:

local player = game.Players.LocalPlayer
local character = player.Character
local Humanoid = player.Character:WaitForChild("Humanoid")
local steps = player.PlayerGui.StepsGUI.Frame.TextLabel
local stagetrans = player.PlayerGui.StageTransfer.CurrentStage
local RunService = game:GetService("RunService")
print(steps.Text)

local RegionFolder = workspace.SafeZones --Folder of regions.
local Regions = RegionFolder:GetChildren() --Region folder's children (regions).

RunService.Heartbeat:Connect(function()
	for _, Region in ipairs(Regions) do
		local Parts = workspace:GetPartsInPart(Region)
		for _, Part in ipairs(Parts) do
			local Model = Part:FindFirstAncestorOfClass("Model")
			if not Model then continue end
			local Humanoid = Model:FindFirstChildOfClass("Humanoid")
			if not Humanoid then continue end
			if not player then continue end
			steps.Text = "inf"
		end
	end
end)

print('made it past')
stagetrans:GetPropertyChangedSignal("Text"):Connect(function()
	if stagetrans.Text == "1" then
		steps.Text = "100"
	elseif stagetrans.Text == "2" then
		steps.Text = "150"
	elseif stagetrans.Text == "3" then
		steps.Text = "125"
	elseif stagetrans.Text == "4" then
		steps.Text = "25"
	elseif stagetrans.Text == "5" then
		steps.Text = "200"
	elseif stagetrans.Text == "6" then
		steps.Text = "250"
	elseif stagetrans.Text == "7" then
		steps.Text = "200"
	elseif stagetrans.Text == "8" then
		steps.Text = "75"
	elseif stagetrans.Text == "9" then
		steps.Text = "125"
	elseif stagetrans.Text == "10" then
		steps.Text = "75"
	elseif stagetrans.Text == "11" then
		steps.Text = "125"
	

	end
end)

print('while loop')
while task.wait() do
	if Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		local new = tonumber(steps.Text)-1
		print(new)
		local text = tostring(new)
		player.PlayerGui.StepsGUI.Frame.TextLabel.Text = text
		if new <= 15 then
			steps.TextColor3 = Color3.new(0.866667, 0, 0.0156863)
			steps.Parent.ImageLabel.ImageColor3 = Color3.new(0.866667, 0, 0.0156863)
		elseif new >= 16 then
			steps.TextColor3 = Color3.new(255,255,255)
			steps.Parent.ImageLabel.ImageColor3 = Color3.new(255,255,255)
		end
		if new <= 0 then
			Humanoid.Health -=100
			if stagetrans.Text == "1" then
				steps.Text = "100"
			elseif stagetrans.Text == "2" then
				steps.Text = "150"
			elseif stagetrans.Text == "3" then
				steps.Text = "125"
			elseif stagetrans.Text == "4" then
				steps.Text = "25"
				break
			end
		end
	end
end
print('script ended')

PS: Somehow it stops at print(‘while loop’) it stops somewhere in the while true loop but idk where.

You can use this tutorial to know when a player enters in a zone

Or you can also check this devforum post

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.