Making a "Spawner" make butterfly's fly away from it

Hello! I am SpiralGaia, a small developer. I do scripting, GFX, UI, and more.

I am trying to achieve a new creation. A spawner if you will, that will make a model (Butterfly) fly from it and outwards until eventually despawning. I also am trying to make the wings flap. I am wondering how I would do this.

I believe I would use

for _,v in pairs(game.Workspace.Spawners:GetChildren()) do

end

for this, but I am not completely sure about it. I want to tween 2 unions (the wings) up and down as it moves, and after a random time (math.random) the butterfly despawns. Also, I would like a max amount of butterflies before the game lags.

TL;DR - Need a brick to make a model slowly fly outwards in a random direction, then despawn.

Thanks!

1 Like

Next time, please try to show some sort of attempt to solve your problem. Also, don’t ask people to write entire systems for you. That said, I very quickly wrote a very basic script that can serve as a foundation.

local Spawners = workspace.Spawners
local Butterfly = game.ReplicatedStorage.Butterfly
local TweenService = game:GetService("TweenService")

for _, spawner in pairs(Spawners:GetChildren()) do
	coroutine.wrap(function() -- each spawner acts independently
		-- spawn every 5-10 seconds
		while wait(5 + math.random(5)) do
			-- Clone butterfly into the spawner
			local fly = Butterfly:Clone()
			fly.Parent = workspace
			fly.Position = spawner.Position
			
			-- Tween the butterfly to a position
			local tween = TweenService:Create(fly, TweenInfo.new(math.random(20)), {Position = fly.Position + Vector3.new(math.random(30), math.random(30), math.random(30))})
			tween:Play()
			-- Have another tween(s) here for the wings
			-- Despawn when tween is finished
			tween.Completed:Wait()
			fly:Destroy()
		end
	end)()
end
4 Likes

Oh. I attempted to write a system, but I forgot to save it when it didn’t work. Sorry bout that.

Why not just rig the butter fly and create an animation then tween it or sum

This was 2 years ago and I was way more unexperienced.

But in regards to your response, I was (at the time) intending to have quite a few butterflies around the map. If I were to have 40+ butterflies tweening around the map and playing animation, the game would be very laggy.

That is true, if you turn on streaming enabled then this can possibly be combated by changing the streaming min max target distance. This way the whole world will not be rendered in or accounted for until the player is in range

1 Like