Don’t misunderstand the title. I’m not talking about an object entering the zone, I’m talking about detecting when an object is inside the zone. So .partEntered isn’t cutting it in this case.
I’m making this topic because I couldn’t find a solution to my problem.
Help would be appreciated!
local PartsInsideZone = Zone:getParts()
for i, Part in pairs(PartsInsideZone) do
if Part.Name == "Sphere" then
print("Sphere is Inside Zone")
else
print("Sphere is NOT Inside Zone")
end
end
This keeps printing out “Sphere is NOT Inside Zone”, am I doing something wrong?
I’m not quite sure if you have multiple spheres, or a singular sphere - but it’s best practice to use the ‘item functions/methods’ rather than the ‘part functions/methods’. Here’s some example code to explain it:
-- services
local serverStorage = game:GetService("ServerStorage")
-- variables
local container = workspace:WaitForChild("Part")
-- modules
local zone = require(serverStorage:WaitForChild("Zone"))
-- functions
-- create a new zone class from a container
local sphereZone = zone.new(container)
-- this function will add an item to the zone to begin tracking it
local function trackSphere(sphere: BasePart)
-- make sure the sphere actually exists
if (not sphere) then return end
-- begin tracking the sphere
sphereZone:trackItem(sphere)
end
-- this function returns all items within the zone, in this case, it just returns all the spheres
local function getSpheresInZone()
return sphereZone:getItems()
end
-- this is a redundant function which creates a sphere. In your case, you'll probably just be using the 'trackSphere(SPHERE)' function
local function createSphere()
-- create a sphere with a few base properties
local sphere = Instance.new("Part")
sphere.Shape = Enum.PartType.Ball
sphere.Size = Vector3.one * 4
sphere.Position = Vector3.new(0, 20, 0)
sphere.Material = Enum.Material.Metal
sphere.Parent = workspace
-- begin tracking the sphere
trackSphere(sphere)
end
-- create a sphere
createSphere()
-- print all the items within the zone every two seconds
while true do
print(getSpheresInZone())
task.wait(2)
end
ZonePlus allows you to track individual objects via the :trackItem method, seen in the documentation. Additionally, the :getItems method will return all tracked items within the zone.
With that information you’re able to create a zone, track the spheres you want, and retrieve the spheres that are present within the zone.
If you have one sphere present, and you want to check if that sphere is within the zone, you can use the code below:
local function getSpheresInZone()
return sphereZone:getItems()
end
if (#getSpheresInZone() > 0) then
print("Sphere(s) in zone")
else
print("No sphere(s) in zone")
end
Just make sure to call :trackItem(sphere) beforehand.
I’m sorry for the late response, I didn’t have access to my computer. I was aware of the :trackItem method and I’ve already tried using it before making this post, but clearly I was doing something wrong. Thank you for your detailed explanation and your help!