What’s happening for you is the following:
This is caused by the exiting properties of one zone being applied after the entering properties of the other (due to the ability to be in more than one zone at once).
local Zone = require(game:GetService("ReplicatedStorage").Zone)
local lightingAreas = workspace.LightingAreas
local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")
local DEFAULT_VALUES = {
FogColor = lighting.FogColor,
FogStart = lighting.FogStart,
FogEnd = lighting.FogEnd,
ClockTime = lighting.ClockTime,
}
local function changeLighting(lightingDictionary)
tweenService:Create(lighting, TweenInfo.new(0.5), lightingDictionary):Play()
end
for _, group in pairs(lightingAreas:GetChildren()) do
local zone = Zone.new(group)
zone.lightingDictionary = {
FogColor = group.Color,
FogStart = 20,
FogEnd = 200,
ClockTime = 0,
}
zone.localPlayerEntered:Connect(function()
changeLighting(zone.lightingDictionary)
end)
zone.localPlayerExited:Connect(function()
changeLighting(DEFAULT_VALUES)
end)
end
The desired outcome is the following:
This is achieved by tracking how many zones we are in, then when the exited
signal is fired for a zone, we first check if we are currently in any other zones. If we are, then apply that zones properties, else only then reset to default.
local Zone = require(game:GetService("ReplicatedStorage").Zone)
local lightingAreas = workspace.LightingAreas
local lighting = game:GetService("Lighting")
local tweenService = game:GetService("TweenService")
local DEFAULT_VALUES = {
FogColor = lighting.FogColor,
FogStart = lighting.FogStart,
FogEnd = lighting.FogEnd,
ClockTime = lighting.ClockTime,
}
local zonesWithin = {}
local zonesWithinCount = 0
local function changeLighting(lightingDictionary)
tweenService:Create(lighting, TweenInfo.new(0.5), lightingDictionary):Play()
end
for _, group in pairs(lightingAreas:GetChildren()) do
local zone = Zone.new(group)
zone:setDetection("Centre")
zone.lightingDictionary = {
FogColor = group.Color,
FogStart = 20,
FogEnd = 200,
ClockTime = 0,
}
zone.localPlayerEntered:Connect(function()
zonesWithin[zone] = true
zonesWithinCount += 1
changeLighting(zone.lightingDictionary)
end)
zone.localPlayerExited:Connect(function()
zonesWithin[zone] = nil
zonesWithinCount -= 1
if zonesWithinCount == 0 then
changeLighting(DEFAULT_VALUES)
else
for zone, _ in pairs(zonesWithin) do
changeLighting(zone.lightingDictionary)
break
end
end
end)
end
We also set the zones detection to ‘Centre’ for the latter example which is more desirable for your situation:
zone:setDetection("Centre")
You can find these examples at the playground within StarterPlayerScripts
:
https://www.roblox.com/games/6166477769/ZonePlus-Playground