How to send more then one variable in a coroutine.wrap?

Hmm… Try out this code in the commandbar so we can locate the issue.

function f(a,b)
print(a,b)
end

local c = coroutine.create(f)
coroutine.resume(f,1,2)

What is in the output? If this prints the numbers 1 and 2, then your code is structured wrong.

The output is this:
local c = coroutine.create(f)
coroutine.resume(f,1,2):6: invalid argument #1 to ‘resume’ (thread expected, got function)
Yes it is an error.

My bad, try

function f(a,b)
print(a,b)
end

local c = coroutine.create(f)
coroutine.resume(c,1,2)

The output is 1 and 2 only and not repeated.

Then that’s the code working properly, meaning something is structured badly in your code. I didn’t manage to find out what, but try not using placeholder parameters and check the actual code running, with the example above implemented?

Call me crazy, but do you need ANY variables for a coroutine wrap? just put it in a function and it will use those variables.

example:

function dosomething (var1, var2)
coroutine.wrap( function ()
–stuff with var1, var2 ) ()
end

That could be possible but it is inefficient and would cause a mess that is hard to add anything or remove anything later on plus the variable constantly changes so you would need a new set of code for each and every possible changes, I don’t know for you but I would not want the code to be that messy and inefficient.

coroutine.wrap(function)(argument1, argument2)

function Tower.Attack(newTower,plr)
	print(plr)
	local Config = newTower.Status
	local target = FindNearestTarget(newTower, Config.Range.Value)
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.PrimaryPart.Position)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
		if Config.Damage.Value > target.Humanoid.Health then
			plr.Money.Value += target.Humanoid.Health
		elseif Config.Damage.Value <= target.Humanoid.Health then
			plr.Money.Value += Config.Damage.Value
		end
		target.Humanoid:TakeDamage(Config.Damage.Value)
		task.wait(Config.FireRate.Value)
	end
	task.wait(0.1)
	Tower.Attack(newTower)
end

function Tower.Spawn(name, cframe, plr)
	local AllowedToSpawn = Tower.CheckSpawn(plr, name)
	if AllowedToSpawn then
		local newTower = RepStorage.Towers[name]:Clone()
		newTower:SetPrimaryPartCFrame(cframe)
		newTower.Parent = workspace.Towers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)
		
		local bodyGyro = Instance.new("BodyGyro")
		bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
		bodyGyro.D = 0
		bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
		bodyGyro.Parent = newTower.HumanoidRootPart
		
		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Tower"
			end
		end
		plr.Money.Value -= newTower.Status.Cost.Value
		plr.TowerPlaced.Value += 1
		coroutine.wrap(Tower.Attack)(newTower,plr)
		else
		warn("Requested tower does not exist", name)
	end
end

Output = nil

Depends on what you are doing, I suppose, but I have not run into any problems and never once used a variable for a wrap. I use it to finish off a function that needs to both return immediately AND wait for time to pass. It happens a lot in my games… I do not use wrap for core game mechanics.

No idea, should’ve worked. That’s how I always used it

Weird that the things that works for others don’t work for me.

function test(A, B, C, D)
	print(A)
	print(B)
	print(C)
	print(D)
end

coroutine.wrap(test)(1, 2, 3, 4)