Making Object Spawn if there are two or less and having them float down to the ground

So essentially I have a script that spawns a healing orb every minute in my arena but I want to make it so no more will spawn if there are 2 already there.
image
(I have them spawn in a heal folder)

I want them to spawn at the top of this part randomly then float down to the ground but I couldn’t figure out the float down part.

In your loop that runs every however long to add a new heal you can simply add a check to get the number of objects in the folder.

loop here:
if #heals:GetChildren() >= 2 then return end

This gets all the items in the folder with :GetChildren() and then gets the total count with #, if the number it returns is greater than or equal to 2 then we can just use return to stop the loop from running for that iteration.

1 Like

If you already have them spawning you can simply use TweenService to make them float down.

local tweenService = game:GetService("TweenService") -- Get TweenService
local healFloatInfo = TweenInfo.new(1) -- Change info like duration, style, and direction here

-- Create and play a tween
local healTween = tweenService:Create(healPartHere, healFloatInfo,
	{CFrame = healPartHere.CFrame-Vector3.new(0,distanceToFloatDownwards,0)})
healTween:Play()

And you can use the method lolbiIlyyy said to limit the amount to 2.

1 Like

both worked great, thanks guys

only issue here is after the parts reach two then it stops the loop I have, even if the parts are destroyed or removed.


fixed it by just removing the return so it doesn’t cancel the loop but I’m not sure if this can cause memory leaks or not

Instead of using if condition then (do nothing) else you can just invert the condition

if #heals:GetChildren() < 2 then
	-- Spawn logic
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.