Does my butterfly move script work?

Is this script functional?

local sphere = script.Parent.Butterfly

function computeDirection(vec)
local lenSquared = vec.magnitude * vec.magnitude
local invSqrt = 1 / math.sqrt(lenSquared)
return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end

function moveButterfly()

local dx = math.random(-100,100)
local dy = math.random(-40,40)
local dz = math.random(-100,100)

if (sphere.Position.y < 10 and dy < 0) then dy = -dy end
if (sphere.Position.y > 80 and dy > 0) then dy = -dy end

local vec = computeDirection(Vector3.new(dx,dy,dz))

sphere.BodyPosition.position = sphere.Position + (vec * 40) -- change this number to alter speed
end

while true do
moveButterfly()
wait(1)
end

I’m not asking if it is well written, I’m asking if it is functional

1 Like

You can just test this in Studio. Drop the code in an appropriate location, run it and check if it’s doing what you want it to do as well as if there’s any errors. Doesn’t need a code review or second opinion.

Hopped this over to support since Code Review is for help improving code that you know for sure works. It’s not a second Cool Creations but for code.

2 Likes