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 9 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:

jumparts={}

Debounce=false
children=game.Workspace:GetChildren()
function onClicked()
      script.Parent.BrickColor=BrickColor.new("Really red")
               for i,v in pairs(children)do
                     if v.Name=="jumpart"then
                        table.insert(jumparts,i,v.Name)
             end
    end


    if not Debounce then

        Debounce=true

             for i,v in pairs(children)do

                if v.Name=="jumpart" then

                      for i=4,13,0.3 do

                         v.Size=Vector3.new(v.Size.x,i,v.Size.z)

                         wait(0.1)

                             end
        wait(1)
            for i=13,4,-0.3 do

                v.Size=Vector3.new(v.Size.x,i,v.Size.z)
                v.Position=Vector3.new(v.Position.x,v.Position.y-4.4,v.Position.z)
                 wait(0.1)

        end


    end


end
    script.Parent.BrickColor=BrickColor.new("Lime green")
    Debounce=false
end


end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
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 — 9y
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 — 9y
0
please help threatboy101 2 — 9y

1 answer

Log in to vote
0
Answered by 9 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:

for i = 1, #jumparts do
--do something to jumparts[i] here, ex:
jumparts[i].Position = jumparts[i].Position + Vector3.new(0,5,0)
end

--or

for i, jumpart in pairs(jumparts) do
--same thing as before, but use "jumpart" instead of "jumparts[i]"
end

A revision of your script:

jumparts={}
children=game.Workspace:GetChildren()
for i,v in pairs(children)do
    if v.Name=="jumpart"then
        table.insert(jumparts,v)
    end
end
Debounce=false

function onClicked()
    if not Debounce then
        Debounce=true
        script.Parent.BrickColor=BrickColor.new("Really red")
        local v
        for i = 4, 13, 0.3 do
            for j = 1, #jumparts do
                v = jumparts[j]
                v.Size=Vector3.new(v.Size.x,i,v.Size.z)
            end
            wait(0.1)  
        end
        wait(1)
        for i=13,4,-0.3 do
            for j = 1, #jumparts do
                v = jumparts[j]
                v.Size=Vector3.new(v.Size.x,i,v.Size.z)
                v.Position=Vector3.new(v.Position.x,v.Position.y-4.4,v.Position.z)
            end
            wait(0.1)
        end
        script.Parent.BrickColor=BrickColor.new("Lime green")
        Debounce=false
    end    
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

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 — 9y
0
Edited chess123mate 5873 — 9y
0
thank you threatboy101 2 — 9y
0
the script is not working and everytime i click i get size is not a valid member of camera threatboy101 2 — 9y
0
Whoops, I had a couple bugs throughout the script. I've updated it. chess123mate 5873 — 9y
Ad

Answer this question