BoundingBox not printing despite nothing being in it

I’m trying to create different lighting environments based on where the player is in the map, so I decided to use a bounding box and a local script because it seemed the easiest solution.

It will print once my character is actually IN the bounding box, but it wont when I’m OUTSIDE it.

local plr = game:GetService("Players").LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()

local zones = workspace:WaitForChild("Zones")
local rs = game:GetService("RunService")
local ts = game:GetService("TweenService")

local lighting = game:GetService("Lighting")

for _, possible_zone in ipairs(zones:GetChildren()) do
	if possible_zone:IsA("BasePart") then
		local touching = false
		local params = OverlapParams.new()
		params.FilterDescendantsInstances = {chr:GetChildren()}
		params.FilterType = Enum.RaycastFilterType.Include
		rs.Heartbeat:Connect(function()
			local boundingbox_parts = game.Workspace:GetPartBoundsInBox(possible_zone.CFrame, possible_zone.Size, params)
			for i,v in pairs(boundingbox_parts) do
				if boundingbox_parts then
					if touching == false then
						touching = true
						print("chr inside box")
						ts:Create(lighting:WaitForChild("DarknessCorrection"),TweenInfo.new(.3, Enum.EasingStyle.Circular, Enum.EasingDirection.Out),{Brightness = -0.02}):Play()
					end
				elseif not boundingbox_parts then
					touching = false
					print("chr outside box")
					ts:Create(lighting:WaitForChild("DarknessCorrection"),TweenInfo.new(.3, Enum.EasingStyle.Circular, Enum.EasingDirection.Out),{Brightness = 0}):Play()
				end
			end
		end)
	end
end

I’m not worried about optimizing it or other specifics; I just want to know how to fix this.

1 Like

fixed it by just changing how it generally worked in general, but if anyone has any better solutions please leave them

local plr = game:GetService("Players").LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()

local zones = workspace:WaitForChild("Zones")
local rs = game:GetService("RunService")
local ts = game:GetService("TweenService")

local lighting = game:GetService("Lighting")

for _, possible_zone in ipairs(zones:GetChildren()) do
	if possible_zone:IsA("BasePart") then
		local touching = false
		local params = OverlapParams.new()
		params.FilterDescendantsInstances = {chr:GetChildren()}
		params.FilterType = Enum.RaycastFilterType.Include
		rs.Heartbeat:Connect(function()
			local boundingbox_parts = game.Workspace:GetPartBoundsInBox(possible_zone.CFrame, possible_zone.Size, params)
			
			if #boundingbox_parts > 0 then
				if touching == false then
					touching = true
					print("chr inside box")
					ts:Create(lighting:WaitForChild("DarknessCorrection"),TweenInfo.new(.3, Enum.EasingStyle.Circular, Enum.EasingDirection.Out),{Brightness = -0.02}):Play()
				end
			elseif #boundingbox_parts <= 0 then
				touching = false
				print("chr outside box")
				ts:Create(lighting:WaitForChild("DarknessCorrection"),TweenInfo.new(.3, Enum.EasingStyle.Circular, Enum.EasingDirection.Out),{Brightness = 0}):Play()
			end
		end)
	end
end