Hello, I am trying to make a vacuum on the ceiling of a room that sucks up items to be deleted. I am using bodypositions to do this, and the way I am doing this is by looping through every unwanted item, making a body position for each item, setting up a reachedtarget connection for each item, and then moving the items one last time before deleting them. reachedtarget only fires when the body position’s part is super close to it’s goal, which rarely actually happens. How could I make an alternative that keeps my code asynchronous while increasing the minimum distance to activate the event?
here is my code:
local function disposeThings()
if isDisposing == true then return end
isDisposing = true
for i,v in pairs(spawnedThings:GetChildren()) do
if v:IsA("BasePart") then
--local goalpos = {}
--goalpos.Position = Vector3.new(0, 11.75, -15.5)
local bp = Instance.new("BodyPosition")
bp.Position = Vector3.new(0, 11.75, -15.5)
bp.Parent = v
v.CanCollide = false
local tempconnect
tempconnect = bp.ReachedTarget:Connect(function()
bp.Position = Vector3.new(0, 16.75, -15.5)
DBS:AddItem(v,2)
tempconnect:Disconnect()
end)
elseif v:IsA("Model") then
for a,b in pairs(v:GetDescendants()) do
if b:IsA("BasePart") then
b.CanCollide = false
end
end
if v.PrimaryPart then
local bp = Instance.new("BodyPosition")
bp.Position = Vector3.new(0, 11.75, -15.5)
bp.Parent = v.PrimaryPart
local tempconnect
tempconnect = bp.ReachedTarget:Connect(function()
bp.Position = Vector3.new(0, 16.75, -15.5)
DBS:AddItem(v,2)
tempconnect:Disconnect()
end)
end
end
end
--[[wait(2)
for i,v in pairs(spawnedThings:GetChildren()) do
if v:IsA("BasePart") then
--local goalpos = {}
--goalpos.Position = Vector3.new(0, 11.75, -15.5)
local bp = v:FindFirstChild("BodyPosition")
bp.Position = Vector3.new(0, 16.75, -15.5)
elseif v:IsA("Model") then
if v.PrimaryPart then
local bp = v.PrimaryPart:FindFirstChild("BodyPosition")
bp.Position = Vector3.new(0, 16.75, -15.5)
end
end
end
wait(1)
for i,v in pairs(spawnedThings:GetChildren()) do
v:Destroy()
end]]--
thingCount = 0
thingCountText.Text = tostring(thingCount).."/"..tostring(maxThingCount)
isDisposing = false
end