Hello, I am quite new and inexperienced with dealing with Region3’s. I’m trying to make a hit detection system but I can’t quite understand how to remove or destroy the region after some time. I used a while loop and a debounce but that is just inefficient and does not even work properly sometimes.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
ReplicatedStorage.Carry.OnServerEvent:Connect(function(player, center)
local region = Region3.new(center - Vector3.new(2,1,2), center + Vector3.new(2,1,2))
local partsInRegion = workspace:FindPartsInRegion3WithIgnoreList(region, {workspace.Baseplate, table.unpack(player.Character:GetChildren())}, 1)
local debounce = false
----------[ VISUALS ]---------
local part = Instance.new("Part")
part.CFrame = region.CFrame
part.Size = region.Size
part.Anchored = true
part.CanCollide = false
part.Transparency = 0.5
part.Parent = workspace
game:GetService("Debris"):AddItem(part, 0.1)
----------[ VISUALS ]---------
while wait(0.1) do
if not debounce then
debounce = true
for i, bodyPart in pairs(partsInRegion) do
local character = bodyPart.Parent
if character:FindFirstChild("Humanoid") then
print("character detected.")
end
end
wait(1)
debounce = false
end
end
end)