#Randomize color only works for part 1, but not for part 2

38 messages · Page 1 of 1 (latest)

woeful ermine
#

because it starts a while and stops there

#

because is in a loop and doesnt reach the second

woven cargo
#

that creates a new thread

#

and the code should continue

woeful ermine
#

yeah well, i'll prefer task never used coroutine

woven cargo
#

ya i would also say use task.spawn(fucntion() instead

delicate glen
#

okay thank you

woven cargo
#

ohhh

#

i just noticed

#

@delicate glen

#

ik why its not working your way

delicate glen
#

why?

round sandBOT
#

studio** You are now Level 12! **studio

woven cargo
#

"Returns a function that resumes the coroutine each time it is called."

#

your calling the exact same function 2 times

delicate glen
#

So how do I use coroutines to make multiple tasks?

#

in the same time

#

and in same script

woven cargo
#
local Part1 = workspace:WaitForChild("Part1")

local Part2 = workspace:WaitForChild("Part2")


function ChangeColor(part)
    while wait(.1) do
        part.Color = Color3.new(math.random(0, 255), math.random(0, 255), math.random(0, 255))
    end
end

coroutine.wrap(function()
  ChangeColor(Part1)
end)()

coroutine.wrap(function()
  ChangeColor(Part2)
end)()
#

this is how you would make it

delicate glen
#

OOOOH

#

Thank you a lot!

woven cargo
#

np

delicate glen
#

I have a last question

#

How do I mark my post as solved?

#

oh nvm

restive dagger
#

task.spawn(ChangeColor, Part1)
task.spawn(ChangeColor, Part2)

delicate glen
#

Okay

#

But

#

When you pause a coroutine with coroutine.Resume

#

It stops and when you resume it so it starts again it does nothing cause the thread is "dead" any idea about how I fix that?

delicate glen
#

Nevermind

#

I managed to do something

#

Very very very cool

#
local Part1 = workspace:WaitForChild("Part1")

local Part2 = workspace:WaitForChild("Part2")

local Tasks = {}

Tasks.Resume = function(func, var)
    return task.spawn(func, var)
end

Tasks.Cancel = function(ResumedTask)
    task.cancel(ResumedTask)
end

local function ChangeColor(part)
    while wait(.1) do
        
        part.Color = Color3.new(math.random(0, 255), math.random(0, 255), math.random(0, 255))
    end
end


local Task1 = Tasks.Resume(ChangeColor, Part1)

local Task2= Tasks.Resume(ChangeColor, Part2)


task.wait(5)

Tasks.Cancel(Task1)

Tasks.Cancel(Task2)

task.wait(3)

Tasks.Resume(ChangeColor, Part1)

Tasks.Resume(ChangeColor, Part2)