Passing table as parameter to coroutine sends copy of table instead

The issue is self explanaotry, I wanted to know if this is standard behaviour and if so how should I circumvent it, here is my code.

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

MoveCube() is the coroutine and is called by cube.SpawnGroup()

Tables are never sometimes (edit: I was wrong thanks @su0002) copied when passed as arguments. Could you put together a minimal self-contained example that demonstrates what you’re asking?

1 Like

Ok, do I make it a .rbxl file? or just the code?

This is what the documentation says

Copies of tables are produced when passed as arguments to or returned from the OnInvoke callback. This means that means that tables passed as arguments will not be exactly equivalent to those provided on invocation, and tables returned to the invoker will not be exactly equivalent to the ones returned by the OnInvoke callback.

Apparently, all arguments passed to Invoke are serialized and deserialized, similar to remotes. Both BindableEvent and BindableFunction are intended to pass information between systems while maintaining their independence similar to what happens with the client and server.

So if you choose to skip that feature, you can implement your own version of BindableFunction. Here is an example of a BindableEvent.

2 Likes

Ah, of course, it wasn’t the coroutine, but raher the bindable function. :man_facepalming: