Hello,
I have a part that clones itself and moves around randomly but when it leaves a specific range it teleport’s all the parts instead of one part to a random position.
My code for spawning it
local Model = script.Parent
center = script.Parent.Center.CenterPart
local part= Model.part:Clone()
Model.ChildRemoved:connect(function()
local newpart = part:Clone()
local x,z = center.Position.X, center.Position.z
local r = 10
local x,z = math.random(x-r, x+ r),math.random(z-r,z+r)
local pos = Vector3.new(x,center.Position.Y,z)
newpart.Position = pos
newpart.Parent = Model
end)
My code to remove the part:
local Range = Model.Configuration.partRange
while true do
wait(5)
if (part.Position - Model.Center.CenterPart.Position).magnitude > Range.Value then
part:destroy()
break
end
end
So thats the issue, it has 6 parts called part.
Look in your output and I would maybe rewrite the script. When my script does not work, I rewrite it because there is most likely a small thing that you do not notice.
part = script.Parent
if (part.Position - Model.Center.CenterPart.Position).magnitude > Range.Value then
part:destroy()
end
This is destroys the fly but it sends the rest to a random position in the center part.
my spawn script:
local Model = script.Parent
center = script.Parent.Center.CenterPart
local part = Model.part:Clone()
Model.ChildRemoved:connect(function()
local newpart = part:Clone()
local x,z = center.Position.X, center.Position.z
local r = 10
local x,z = math.random(x-r, x+ r),math.random(z-r,z+r)
local pos = Vector3.new(x,center.Position.Y,z)
newpart.Position = pos
newpart.Parent = Model
end)