Hello, I have been working on my game and I had to call a finciton 3 times so I did:
viewport(petdisplay, chosenPet:Clone())
viewport(petdisplayRight, chosenPet:Clone())
viewport(petdisplayLeft, chosenPet:Clone())
The function is a normal function but it has some waits in it and I was wondering how can I skip that wating time ?
Thank you !
spawn(function()
--function in here
end)
someones going to say use couroutines but idk how (this is how i do it)
2 Likes
njesk12
(njesk12)
March 17, 2021, 9:20pm
#3
Coroutines! Read about them here
Thank you ! But how can I name the function with spawn()
That is the same as
coroutine.wrap(function()
end)()
or
coroutine.resume(coroutine.create(function(l
end))
2 Likes
local function func()
end
spawn(func())
2 Likes
Don’t call the function. Just insert it as a parameter.
local function func()
end
spawn(func)
2 Likes
And how do I send a parameter in the function with that ?
njesk12
(njesk12)
March 17, 2021, 9:33pm
#9
Using coroutine, you can do:
function foo(someString)
print (someString)
end
local newCoroutine = coroutine.wrap(foo)
newCoroutine("Hello, World!")
2 Likes
You can’t. You would have to use coroutines which pretty much have the same result.
local function func(number)
print(num)
end
coroutine.wrap(func)(3)
And to call it 2 times I do this ?
local newCoroutine = coroutine.wrap(viewport)
newCoroutine(petdisplay, chosenPet:Clone())
newCoroutine(petdisplayRight, chosenPet:Clone())
It gives me an error when I call it 2 times here is my code:
game.ReplicatedStorage.Remotes.TripleHatchClient.OnClientInvoke = function(eggName)
local module = require(workspace.Eggs:FindFirstChild(eggName).Rarity)
local chosenPet = module.randPet()
local chosenPet2 = module.randPet()
print(chosenPet2)
local egg = workspace.Eggs:FindFirstChild(eggName)
renderEgg(egg, middleEgg)
renderEgg(egg, leftEgg)
renderEgg(egg, rightEgg)
middleEgg.Visible = true
leftEgg.Visible = true
rightEgg.Visible = true
script.Parent.Parent.Main.Enabled = false
wait(eggSpeed)
middleEgg.Visible = false
leftEgg.Visible = false
rightEgg.Visible = false
local newCoroutine = coroutine.wrap(viewport)
newCoroutine(petdisplay, chosenPet:Clone())
newCoroutine(petdisplayLeft, chosenPet2:Clone())
template(chosenPet)
petdisplay.Visible = true
return chosenPet
end
The print works so it does not return nil but it gives me an error saying attempt to call a nil values (without the line where the error is).
You can’t store a coroutine if you want it to run at the same time.
Instead of
Do
coroutine.wrap(viewport)(petdisplay, chosenPet:Clone())
coroutine.wrap(viewport)(petdisplayLeft, chosenPet2:Clone())
2 Likes
njesk12
(njesk12)
March 17, 2021, 9:43pm
#14
If you want them to run simultaneously, you’d have to write different coroutine.wraps. Instead of declaring the wrap as a variable call them each individually:
coroutine.wrap(viewport)(arg1, arg2, ...)
coroutine.wrap(viewport)(arg1,arg2,...)
Otherwise it’ll wait for the first thread to finish and then run the second call
^^^ @myaltaccountsthis
2 Likes
All of these work I don’t know wich one to mark as a solution ! Thank you all very much!