generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Why is this script doing this (coroutines)

Previous Thread :: Next Thread 
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 05:26 PM
Vague title, sorry.
It is like a music reader, but each coroutine seems to be only reading the first row of notes, and it plays that row really quickly and then when it's done it says
18:13:58 - attempt to call a nil value
18:13:58 - cannot resume non-suspended coroutine
but no line numbers

row1 = {}
row2 = {}
row3 = {}
position = 1
for i,v in pairs(script.Parent.Notes:GetChildren()) do
wait()
if v.Position.Y > script.Parent.Reference1.Position.Y - .5 and v.Position.Y < script.Parent.Reference1.Position.Y + .5 then
if v.Position.Z > script.Parent.Reference1.Position.Z - .5 and v.Position.Z < script.Parent.Reference1.Position.Z + .5 then
table.insert(row1,v)
end
end
end
for i,v in pairs(script.Parent.Notes:GetChildren()) do
wait()
if v.Position.Y > script.Parent.Reference2.Position.Y - .5 and v.Position.Y < script.Parent.Reference2.Position.Y + .5 then
if v.Position.Z > script.Parent.Reference2.Position.Z - .5 and v.Position.Z < script.Parent.Reference2.Position.Z + .5 then
table.insert(row2,v)
end
end
end
for i,v in pairs(script.Parent.Notes:GetChildren()) do
wait()
if v.Position.Y > script.Parent.Reference3.Position.Y - .5 and v.Position.Y < script.Parent.Reference3.Position.Y + .5 then
if v.Position.Z > script.Parent.Reference3.Position.Z - .5 and v.Position.Z < script.Parent.Reference3.Position.Z + .5 then
table.insert(row3,v)
end
end
end
table.sort(row1,function(a,b) return a.Position.X > b.Position.X end)
table.sort(row2,function(a,b) return a.Position.X > b.Position.X end)
table.sort(row3,function(a,b) return a.Position.X > b.Position.X end)
co = coroutine.create(function(table)
for i,v in pairs(table) do
if position < 59 then
v.Sound:Play()
v.CFrame = v.CFrame + Vector3.new(0, 0, .1)
wait(v.Wait.Value)
v.CFrame = v.CFrame + Vector3.new(0, 0, -.1)
position = position + 1
end
end
end)
script.Parent.Playbutton.BrickColor = BrickColor.new("Bright red")
coroutine.resume(co, row1)
coroutine.resume(co, row2)
coroutine.resume(co, row3)
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 05:43 PM
local function newThread(foo, ...)
return coroutine.resume(coroutine.create(foo), ...)
end

coFunc = function(table)
for i,v in pairs(table) do
if position < 59 then
v.Sound:Play()
v.CFrame = v.CFrame + Vector3.new(0, 0, .1)
wait(v.Wait.Value)
v.CFrame = v.CFrame + Vector3.new(0, 0, -.1)
position = position + 1
end
end
end
script.Parent.Playbutton.BrickColor = BrickColor.new("Bright red")
newThread(co, row1)
newThread(co, row2)
newThread(co, row3)


Before you were trying to resume a in-progress thread. Imagine it like

*thread1*

resume(thread1, arg1)
resume(thread1, arg2)
resume(thread1, arg3)

Though you're suppying different arguments its still the same thread. What you needed to do was:

*thread_Function_*

resume(newThread(thread_Function_), arg1)
resume(newThread(thread_Function_), arg2)
resume(newThread(thread_Function_), arg3)

Its pretty simple but easy to get confused. I made the function at the top to transform:

resume(newThread(func), args)

to:

resume_newThread(func[, args])

Its really just to make things easier to type but in most cases ther are interchangeable. Anyways hope this helped
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 05:54 PM
Thanks but I don't really understand anything other than
Before you were trying to resume a in-progress thread. Imagine it like

*thread1*

resume(thread1, arg1)
resume(thread1, arg2)
resume(thread1, arg3)

Though you're suppying different arguments its still the same thread. What you needed to do was:

*thread_Function_*

resume(newThread(thread_Function_), arg1)
resume(newThread(thread_Function_), arg2)
resume(newThread(thread_Function_), arg3)
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 06:01 PM
"I made the function at the top to transform:

resume(newThread(func), args)

to:

resume_newThread(func[, args])

Its really just to make things easier to type but in most cases ther are interchangeable. "


you don't get that part or what i did with the actual code?
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 06:05 PM
I don't understand why you did it, and what it means. Anyways, I tried your script out and on the line that says
return coroutine.resume(coroutine.create(foo), ...)
I got this error, also, the way you did it is a lot slower than the previous way, is that just my computer or...?
Workspace.Studio3.Reader:33: bad argument #1 to 'create' (Lua function expected)
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 06:08 PM
oops forgot i changed the variable name:


newThread(coFunc, row1)
newThread(coFunc, row2)
newThread(coFunc, row3)

should work
ehh let me see if i can explain it clearer
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 06:14 PM
It just stopped randomly in the middle so I'm guessing that has something to do with the position < 59 thing, I thought coroutines made all the variables in the script local inside themselves and wouldn't change the outside variables?

Also I'm having the same problem that I was having before I switched to using coroutines, before I had 3 separate scripts each running their own row. The problem is the notes get out of sync and start to sound choppy, like they aren't playing at the same time. How come the coroutine didn't fix this?
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 06:37 PM
No what you did was

Global_Num = 1

function a()
Global_Num = Global_Num+1
print("Function A"..tostring(Global_Num))
end
function b()
Global_Num = Global_Num+1
print("Function B"..tostring(Global_Num))
end

while Global_Num < 15 do
a()
b()
end


local variables created in coroutines are restricted to their enviornment but accessing a variable from outside will not create a new instance of the variable

What you should do is:

Global_Num = 1

function Count(name)
local Local_Num = 1
return function()
Global_Num = Global_Num+1
print("Function "..name.." "..tostring(Global_Num))
end

a = Count("A")
b = Count("B")

while Global_Num < 15 do
a()
b()
Global_Num = Global_Num+1
end

What this does is return a function with a new instance of Local_Num when the function is called. Local Num is restricted to its enviorment so it doesn't affect other functions using the variable ehh should i explain that more? may be a bit confusing at first.. Also about the syncing problem you could try creating a thread schedular(spelt wrong?? oh well...) or maybe rewrite your function to play different notes at once?
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 07:05 PM
Could you help me rewrite it to do that? Also, remember, if playing 3 notes at one time, each of them could have a different wait value. And what is a thread scheduler?
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 07:07 PM
Also couldn't I just move position = 1 to inside the coroutine function, before the if statement checking on position?
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 07:16 PM
no because that would reset position each time the function is called.. you could do

pA = 1
pB = 1
pC = 1
pD = 1
pE = 1

but the repetition can become a bit excessive

Also they all have the same wait time?? and i'm starting to think what you meant to do is more along the lines of

n = 1
function Play()
--code

Run SoundA
newThread(Run SoundB)
newThread(Run SoundC)

--code
n = n+1
end

repeat Play() until n == 50


instead of:

n = 1
function Play1()
Run Sound
end
function Play2()
Run Sound
end
...

repeat Play1() newThread(Play2) --[[ect]] until n == 50

??
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 07:17 PM
wait i think i mis read your code ._. brb
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 07:23 PM
I want position to be reset each time the function is called
so there are 3 rows with notes on them, sixteenth, eighth, quarter, and row1 is the first row's notes and it is read from left to right.
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 07:27 PM
ehh you meant for them to play at the same time right?
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 07:28 PM
right, if you want to see it it's my place called "In progress"
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 07:43 PM
the place is private..? anyways how about this?

function Play(reps, table)

for i = 1, # reps do
local continue = false
for k,v in pairs(table) do
coroutine.resume(coroutine.create(function()
v.Sound:Play()
v.CFrame = v.CFrame + Vector3.new(0, 0, .1)
wait(v.Wait.Value)
v.CFrame = v.CFrame + Vector3.new(0, 0, -.1)
continue = true
end))
end
repeat wait() until continue
end

end

script.Parent.Playbutton.BrickColor = BrickColor.new("Bright red")
Play(#row1, {row1, row2, row3})
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 07:59 PM
How is it different from mine? also rather than #reps (which doesn't make sense) just do #table
I'll try it out though and see if it does anything different.
Thanks for sticking with me, also, I'll uncopylock it, studio3 contains the reader that is updated the most, and studio1 contains my previous methods
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 08:05 PM
Oops just erase the # sign and put reps there. Also instead of running the whole function on 3 different threads i ran the part of code where they play in seperate threads and wait for the threads to finish before going to the next loop. not much changed but should keep them in sync
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 08:09 PM
I didn't understand what the continue stuff was supposed to do, so here is what I have now, I'm about to test it

function Play(table)
for i = 1, #table do
for k,v in pairs(table) do
coroutine.resume(coroutine.create(function()
v.Sound:Play()
v.CFrame = v.CFrame + Vector3.new(0, 0, .1)
wait(v.Wait.Value)
v.CFrame = v.CFrame + Vector3.new(0, 0, -.1)
end))
end
end
end
script.Parent.Playbutton.BrickColor = BrickColor.new("Bright red")
Play({row1, row2, row3})

I think you don't understand what is happening still so maybe you should take a look at my place
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 08:14 PM
Place is still private..?

Also the continue was to make sure the sound finished block of code finished running so that the sounds stayed in sync. An you weren't really supposed to remove the rep either or at least do

for i = 1, #table[1] do

As #table is 3 (row1, row2, row3)
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 08:14 PM
Okay ^ that did absolutely nothing
and what is reps?
the number of notes, right?
that is just #row1 will find the number of notes in row1
#row2 = row2
#row3 = row3
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 08:16 PM
Wow I completely forgot to change the place to uncopylocked... Looks like another profile view for me heh heh. But seriously I don't know how I forgot so quickly. That bothers me... And my last reply was a reply to my post, not yours
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 08:18 PM
Oh you wanted me to do tables within a table. Table-ception? I have 3 separate tables for the 3 rows, so how do I combine them into one table?
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
10 Jul 2012 08:19 PM
Play({row1, row2, row3})

lol
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
10 Jul 2012 08:26 PM
No I mean like create tables within a table, the way you said it had each of the rows inside of the table, but the way I have it they are just separate tables.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image