So I have this code that utilizes a custom “Path” class to move groups of parts along a set path
local function MoveCube(Cube , Path)
print(PreviousPath == Path , PreviousPath , Path)
PreviousPath = Path
table.insert(Path.ToMove , Cube)
Path.Distances[Cube] = 0
coroutine.yield()
print("Returning cube")
table.remove(Path.ToMove , table.find(Path.ToMove , Cube))
Path.Distances[Cube] = nil
for i , v in pairs(Cube:GetChildren()) do
if v:IsA("Decal") then
v:Destroy()
end
end
print("Returning cube")
Cube.CubeCache:ReturnPart(Cube)
end
function cube.SpawnGroup(Group , Path)
print(Path , debug.traceback())
local Map = Path.Map
local Props = cube.CubeProperties[Group.Type]
for i = 1 , Group.Amount do
local newCube = cube.CubeCache:GetPart()
newCube.Size = Vector3.new(Props.Size,Props.Size,Props.Size)
newCube:PivotTo(Map.StartPart:GetPivot())
newCube.Name = Group.Type
newCube.Parent = Map.Cubes
newCube.Color = Props.Color
newCube.SelectionBox.Color3 = Props.Color:lerp(CubeBorderDefaultColor , 0.2)
newCube.SpeedValue.Value = Props.Speed
if Props.TextureId then
for i , face in pairs(Enum.NormalId:GetEnumItems()) do
local decal = Instance.new("Decal")
decal.Transparency = 1
decal.Texture = Props.TextureId
decal.Face = face
decal.Parent = newCube
end
end
--print(PreviousPath == Path , PreviousPath , Path)
--PreviousPath = Path
newCube.ChangeMoveInfo.OnInvoke = coroutine.wrap(MoveCube) --Set the coroutine that changes the cube's move direction when it reaches a waypoint
newCube.ChangeMoveInfo:Invoke(newCube , Path)
game.ReplicatedStorage.Events.DisplayCube:FireAllClients(newCube) --Not sure why this works, but performance seemed to improve
task.wait(Group.Spacing)
end
end
My problem is that if I were to create a variable one scope above the MoveCube()
function, and check if it is the same as the next path passed, it will always return false, even though there is only one instance of the class, and I am sure of that, doin the same thing in the SpawnGroup()
function returns true, so I suspect it has something to do with coroutines