local collectionservice = game:GetService("CollectionService")
local zones = workspace:WaitForChild("Zones")
local parts = collectionservice:GetTagged("Zone")
local tweenservice = game:GetService("TweenService")
local runservice = game:GetService("RunService")
local function inZone(zone)
if not zone.Ambience.Playing then zone.Ambience.Ambience:Play() end
tweenservice:Create(zone.Ambience.Ambience, TweenInfo.new(0.2), {Volume = 1}):Play()
end
local function leftZone(zone)
tweenservice:Create(zone.Ambience.Ambience, TweenInfo.new(0.2), {Volume = 0}):Play()
end
runservice.RenderStepped:Connect(function()
for i, zone in pairs(parts) do
print(zone)
local region = Region3.new(zone.Position - zone.Size/2, zone.Position + zone.Size/2)
local regionparts = workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
for _, zone in pairs(regionparts) do
if zone:FindFirstAncestor(game.Players.LocalPlayer.Name) then
inZone()
break
else
leftZone()
end
end
end
end)
You’re calling the inZone() function with no arguments, so of course inside the function zone is nil. Also, you have a for loop inside another for loop that are both trying to use ‘zone’ as a loop variable. Best not to shadow variables like this, it’s a very easy way to make bugs.
local collectionservice = game:GetService("CollectionService")
local zones = workspace:WaitForChild("Zones")
local parts = collectionservice:GetTagged("Zone")
local tweenservice = game:GetService("TweenService")
local runservice = game:GetService("RunService")
local inside = false
local function inZone(zone)
if not zone.Ambience.Ambience.Playing then zone.Ambience.Ambience:Play() end
tweenservice:Create(zone.Ambience.Ambience, TweenInfo.new(0.2), {Volume = 1}):Play()
end
local function leftZone(zone)
tweenservice:Create(zone.Ambience.Ambience, TweenInfo.new(0.2), {Volume = 0}):Play()
end
runservice.RenderStepped:Connect(function()
for i, zone in pairs(parts) do
local region = Region3.new(zone.Position - zone.Size/2, zone.Position + zone.Size/2)
local regionparts = workspace:FindPartsInRegion3WithWhiteList(region, game.Players.LocalPlayer.Character:GetDescendants())
for _, zoneregion in pairs(regionparts) do
if zoneregion:FindFirstAncestor(game.Players.LocalPlayer.Name) then
inZone(zone)
print("in")
break
else
leftZone(zone)
print("out")
end
end
end
end)