I was wondering how I would go about making boundaries for the model that I'm working on. I am making a butterfly that flies around randomly. My issue is that it keeps on flying away from the baseplate making it so that it isn't visible. It also flies through parts meaning I can't easily contain it. I was wondering how I would go about setting up boundaries that the butterfly doesn't fly through. I have attempted it so many times and nothing has been working. Here is my latest attempt -
local Body = script.Parent.PrimaryPart -- Defined so I can easily use the CFrame
local region = Region3.new(Vector3.new(-270, -8, -270), Vector3.new(270, 100, 270)) -- The region the butterfly can fly in
local b1 = game.Workspace.ButterflyBoundaries.Baseplate -- A Directory of folders wher I have the boundaries which are parts. (Not the actual baseplate)
local boundary = {
b1:WaitForChild("boundary1"), b1:WaitForChild("boundary2"),
b1:WaitForChild("boundary3"), b1:WaitForChild("boundary4"),
b1:WaitForChild("boundary5"), b1:WaitForChild("boundary6")
} -- Each of these refers to each of the 6 parts
while true do
wait()
local partsinregion = workspace:FindPartsInRegion3(region, nil, math.huge) -- Checks if parts are in region
for i, part in pairs(partsinregion) do
if part.Parent:FindFirstChild("ConfirmedButterfly") then -- Checks if the part is a butterfly (Each butterfly has a part on them named ConfirmedButterfly)
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(math.random(-5,5)), math.rad(math.random(-5,5)), math.rad(math.random(-5,5)))) -- Makes the butterfly face a slightly different direction randomly
wait()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(1,0,0)) -- Makes the butterfly fly in the direction its facing
end
boundary[1].Touched:Connect(function() -- From here on was my latest attempt at making the boundaries, it is also repeated for each part.
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0)))
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0))
end)
boundary[2].Touched:Connect(function()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0)))
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0))
end)
boundary[3].Touched:Connect(function()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0)))
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0))
end)
boundary[4].Touched:Connect(function()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0)))
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0))
end)
boundary[5].Touched:Connect(function()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0)))
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0))
end)
boundary[6].Touched:Connect(function()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(0), math.rad(180),math.rad(0)))
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(3,0,0))
end)
end
end
and here is the script without the boundaries
local Body = script.Parent.PrimaryPart -- Defined so I can easily use the CFrame
local region = Region3.new(Vector3.new(-270, -8, -270), Vector3.new(270, 100, 270)) -- The region the butterfly can fly in
while true do
wait()
local partsinregion = workspace:FindPartsInRegion3(region, nil, math.huge) -- Checks if parts are in region
for i, part in pairs(partsinregion) do
if part.Parent:FindFirstChild("ConfirmedButterfly") then -- Checks if the part is a butterfly (Each butterfly has a part on them named ConfirmedButterfly)
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.Angles(math.rad(math.random(-5,5)), math.rad(math.random(-5,5)), math.rad(math.random(-5,5)))) -- Makes the butterfly face a slightly different direction randomly
wait()
script.Parent:SetPrimaryPartCFrame(Body.CFrame*CFrame.new(1,0,0)) -- Makes the butterfly fly in the direction its facing
end
end
end
If anyone could tell me how I would go about setting up boundaries, or even potentially making it so the butterfly can't fly through parts that would be amazing. Or if there is any resources that could help me that I haven't found yet. I'm fairly new to scripting and this is my first full fledged script I've written. Thank you in advance.

3 comments
It comes up with the error 15:59:33.448 - Expected identifier when parsing expression, got 'if'
It happens line 6 where it says
until ifNo that was a mistake I fixed it.