Hopefully the title is somewhat useful, its very generic, but its pretty much my issue to a tee.
long story short, im using OverlapParams to make safezones for my game.
local Part = game.workspace.Protection:GetChildren()
RunService.Heartbeat:Connect(function()
local TouchingParts = workspace:GetPartsInPart(Part, Params)
if #TouchingParts > 0 then
print('player is in safezone')
--return
end
end)
(params are setup properly, thats y they arent shown here)
Part is a folder of parts that are just transparent boxes (the zones)
When part is = to
local Part = game.workspace.Protection:GetChildren()
i get the error
Unable to cast value to Object
However, when when part is equal to
local Part = game.workspace.Protection.Box1
My code works perfectly. My issue lies within trying to get the multiple parts from the folder.
That error occurs because the first argument of the :GetPartsInPart() method accepts an Instance, whereas game.workspace.Protection:GetChildren() returns a table containing all the Objects on the first layer of that Instance.
From my understanding, in order to check if any of the parts within the folder are intersecting any other parts, a loop would need to be utilized. Here’s an example (placebo code):
(I haven’t used these methods in any projects yet so I’m not sure how performant this might be for your case/if there are other ways of achieving this that I may not be aware of at the moment.)
local partsFolder = Workspace.Protection
RunService.Heartbeat:Connect(function()
local moreThanOneTouchingPart = false -- Variable that will be set to true if any of the parts in the folder are intersecting any other parts
for _, part in ipairs(partsFolder:GetChildren()) do -- Loops through the folder that contains the parts
if part:IsA("BasePart") then -- If the Instance within the folder on this iteration of the loop is a BasePart, then...
local TouchingParts = workspace:GetPartsInPart(part, Params) -- Checks if the part at this iteration of the loop is intersecting anything
if TouchingParts > 0 then -- If the part was intersecting any other part, then...
moreThanOneTouchingPart = true -- Sets the variable to true
break -- Stops the loop from continuing because one of the parts is intersecting something in the safezone
end
end
end
if moreThanOneTouchingPart == true then
print("player is in safezone")
--return
end
end)
Side Note: I’m not sure if it’s possible for anything other than a player’s Character to enter the safezone or if you’re implemented additional checks outside of this to ensure it’s a player’s Character but if you haven’t, I’d suggest calling :GetPlayerFromCharacter(part.Parent) to verify that is the case before continuing with the function.