Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to implement table into a loop?

Asked by 10 years ago

the first part of the post is linked below it will explain the purpose of the script though i added somethings in the script below that makes the block go down but it raises and descends the blocks one by one i tried adding a table but i was in the dark when it came to actually implementing it.

https://scriptinghelpers.org/questions/16263/rising-block-script-not-working

The script:

01jumparts={}
02 
03Debounce=false
04children=game.Workspace:GetChildren()
05function onClicked()
06      script.Parent.BrickColor=BrickColor.new("Really red")
07               for i,v in pairs(children)do
08                     if v.Name=="jumpart"then
09                        table.insert(jumparts,i,v.Name)
10             end
11    end
12 
13 
14    if not Debounce then
15 
View all 49 lines...
0
I don't understand your question. You're already using tables in a loop. What is it the script is or isn't doing? chess123mate 5873 — 10y
0
no i used a generic loop to get all the parts but idk how to actually raise all the blocks in that loop threatboy101 2 — 10y
0
please help threatboy101 2 — 10y

1 answer

Log in to vote
0
Answered by 10 years ago

You should only fill the "jumparts" table once, unless more "jumpart" objects appear in the workspace over time.

To go over the list of "jumparts" you've created, do:

01for i = 1, #jumparts do
02--do something to jumparts[i] here, ex:
03jumparts[i].Position = jumparts[i].Position + Vector3.new(0,5,0)
04end
05 
06--or
07 
08for i, jumpart in pairs(jumparts) do
09--same thing as before, but use "jumpart" instead of "jumparts[i]"
10end

A revision of your script:

01jumparts={}
02children=game.Workspace:GetChildren()
03for i,v in pairs(children)do
04    if v.Name=="jumpart"then
05        table.insert(jumparts,v)
06    end
07end
08Debounce=false
09 
10function onClicked()
11    if not Debounce then
12        Debounce=true
13        script.Parent.BrickColor=BrickColor.new("Really red")
14        local v
15        for i = 4, 13, 0.3 do
View all 35 lines...

Notice how we order the loops: we put the 'for i = 4, 13, 0.3 do' loop on the outside and the "for j = 1, #children do" loop on the inside. This means that for each 'i', we'll apply the change to all jumparts, and then we will wait for 0.1 seconds before advancing on to the next 'i'.

Note that this script assumes that all jumparts will be available and loaded right when the script starts running (if the script works in solo but not online, then you need to add a wait command before the script starts running; ideally you keep waiting until the parts you need have been loaded).

If the list of jumparts may change over time, you need to get a fresh list of children from the workspace every time the onClicked function is called.

[EDIT I finished the revision of the script]

0
i gladly accept your answers but please tell me where each of your loops go in replacement of what i did. threatboy101 2 — 10y
0
Edited chess123mate 5873 — 10y
0
thank you threatboy101 2 — 10y
0
the script is not working and everytime i click i get size is not a valid member of camera threatboy101 2 — 10y
0
Whoops, I had a couple bugs throughout the script. I've updated it. chess123mate 5873 — 10y
Ad

Answer this question