The way to send a single variable through a coroutine function is through the following code–
coroutine.wrap(function.move)(EndCFrame)
But I wish to send more than one variable though it so how do I do it?
The way to send a single variable through a coroutine function is through the following code–
coroutine.wrap(function.move)(EndCFrame)
But I wish to send more than one variable though it so how do I do it?
coroutine.wrap(function.move)(EndCframe,AnotherVariable)
?
Tried but it return with a nil.
How about
local c = coroutine.create(function.move)
coroutine.resume(c,EndCframe,AnotherVariable)
?
Still a nil and not the intended variable value.
Then there is something wrong with your function, not coroutine.wrap
. Send the code you call the function from and the function itself. (function.move
)
Also, task.spawn
does the same thing:
task.spawn(function.move, EndCframe, AnotherVariable)
I believe another way is to send a dictionary, but I suggest to do what @majdTRM said.
coroutine.wrap(function.move)({EndCFrame = EndCFrame, SomethingElse = 2})
Here is the function being coroutined:
function 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
And here is the code from the function that is it called from:
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
What about the place where you create it? (Coroutine wrap it)
It is included with the other one just now.
Does making the variables a dictionary work…?
The second (or the wanted variable) comes from another script. Plus, the two variable would constantly be changed so making it a dictionary would not be viable.
What does that mean, usage of BindableEvents?
No it is the usage of BindableFunction between a server script and a local script. However, the code is working perfectly and the variable is the intended value but just unable to send to the other function.
For clarification, is the Tower.Spawn function the function.move function?
No, the Attack function is the function.move function and the Tower.Spawn function is just the part where the coroutine code is located in.
Then I believe you’re giving the wrong parameters to function.move, as it would appear to need a Tower model and a player, not a CFrame
The CFrame is just for placeholder for the actual variable and it is not used in the actual code.
So the newTower variable isn’t nil? Trying out the create-resume method, passing… let’s say, a number as a parameter as the plr variable would still be nil?
Yes the newTower variable isn’t nil but the plr variable is nil to the other function even if it is a number.