Having an NPC fly up and down

i’m trying to make a firefly NPC, but i only know how to have it walk on the ground. does anyone know how i can have it be up in the air and move up and down at random times?

1 Like

Hi I just saw your topic ! So sorry that no one replied to you hope you didnt give up on your project…

Heres what I did
FireFlies1

local TweenService = game:GetService("TweenService")
local MaxDistanceFromInitial = 3 -- Maximum distance from initial position
local Height = 4
local InitialPositions = {} -- Table to store initial positions of parts

-- Function to create tweens for individual parts
local function movePart(part)
	while true do
		local b = math.random(1,8)
		local targetPosition = part.Position

		if b == 1 then
			targetPosition = targetPosition + Vector3.new(3, 0, 0)
		elseif b == 2 then
			targetPosition = targetPosition + Vector3.new(-3, 0, 0)
		elseif b == 3 then
			targetPosition = targetPosition + Vector3.new(0, 0, 3)
		elseif b == 4 then
			targetPosition = targetPosition + Vector3.new(0, 0, -3)
		elseif b == 5 then
			targetPosition = targetPosition + Vector3.new(3, 0, 3)
		elseif b == 6 then
			targetPosition = targetPosition + Vector3.new(-3, 0, -3)
		elseif b == 7 then
			targetPosition = targetPosition + Vector3.new(-3, 0, 3)
		elseif b == 8 then
			targetPosition = targetPosition + Vector3.new(3, 0, -3)
		end

		-- Clamp the target position within the maximum distance from initial position
		local displacement = targetPosition - InitialPositions[part]
		if displacement.magnitude > MaxDistanceFromInitial then
			targetPosition = InitialPositions[part] + displacement.unit * MaxDistanceFromInitial
		end

		local tweenInfo = TweenInfo.new(1.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)

		local tween = TweenService:Create(part, tweenInfo, {Position = targetPosition})
		tween:Play()

		tween.Completed:Wait()
	end
end

-- Function to initialize parts and start their movement
local function initGroupParts(group)
	for _, part in ipairs(group:GetChildren()) do
		if part:IsA("BasePart") then
			InitialPositions[part] = part.Position
			coroutine.wrap(movePart)(part)
		end
	end
end

-- Specify the group you want to apply the script to
local group = game.Workspace.FireFlies

-- Initialize and start movement for parts in the group
initGroupParts(group)

Here’s how it looks

1 Like

I think he would’ve either solved the problem, or gave up with the game two years ago.

:sob:

2 Likes

yea but still theres hope… hope he completes what he wanted to

1 Like

I know this is an old topic, but if anyone finds this topic in the future and wants do something like this, I suggest making a table instead of creating if statements for what a random number is. That way you can make the possible positions bigger or smaller without having to modify a long if-else chain.

for instance, something like this:

-- ... the start of the code
local positions = {Vector3.new(3, 0, 0), Vector3.new(-3, 0, 0)) -- .. so on
local index = math.random(1, #positions) -- random number from index 1 to the maximum index

targetPosition += positions[index]
-- ... the rest of the code

This makes your code far neater and easier to update, and you can format the table with new lines for readability if you want.

2 Likes