#extremely basic for loop not working???

1 messages · Page 1 of 1 (latest)

chrome lagoon
#

Basically I'm scripting a tutorial level.
The "setBeam()" function just creates arrows for the player to follow.
As you complete a task, it will set the beam to the next part of the tutorial.

--...

--Variables that matters here
local interact = workspace:WaitForChild("Interact")
local tutorialParts = interact:WaitForChild("Items"):WaitForChild("TutorialParts")

--...

--Function that matters here
local function setBeam(player, enabled, level)
  if level == 1 and enabled == true then
  beam.Attachment0 = level1        
  elseif level ~= 1 and enabled == true then
    print("test")                                --TEST ONE
    for _, parts in tutorialParts:GetChildren() do
      print("test1")                             --TEST TWO
      if parts.Name == tostring(level) then
        beam.Attachment0 = parts.Attachment
      end
    end        
  elseif enabled == false then
    beam.Attachment0 = nil
  end
end

the script works perfectly EXCEPT this line

for _, parts in tutorialParts:GetChildren() do

I have marked it with print("test") and print("test1") and only the first print actually prints.

#

extremely basic for loop not working???

chrome lagoon
#

i have also tried putting the the interact and tutorialParts variable inside the function aswell and it still doesnt run

void jacinth
#

workspace parts dont always exist immediately

#

you could do it the lazy way or the correct way

#

the correct way is to listen for childadded or use collectionservice

#

the lazy way is to make it a model with atomic streamingmode

chrome lagoon
#

thankyou ill give these a try

#

i thought waitforchild would of been enough

#

theres no errors either usually i get a 'infinite yeild' error or something with waitforchild

void jacinth
#

waitforchild does not also wait for its descendants

chrome lagoon
void jacinth
#

in essence you need to waitforchild for all of the folder's descendants.

chrome lagoon
#

okk too easy i can do this within the client script in playerstarter

void jacinth
chrome lagoon
#

cheers for the help btw

chrome lagoon
#

sorry i had to run down to the shops
i just now tested i added "workspace.Interact.Items.Tutorial.ChildAdded:Wait()" and it works perfectly now thankyou!

void jacinth
#

should connect, not wait

#

coz if they happen to all stream in early, this will wait forever

chrome lagoon
#

so i had to mess with it a bit, the initial problem is that i call this function every time the datastore updates the tutorial level and will prevent it from being called due to the childadded function. so i decided to go with

repeat wait() until #tutorialParts:GetChildren() >= 1
``` this makes sure there's atleast one part loaded but after that will pretty much not wait at all.
#

i did try to use childadded:connect... but the only place i could put it without it interfering with the game was in the dataInit server script.. which defeats the purpose of trying to load parts on the client

void jacinth
#

just make the folder a model with atomic streamingmode if you're going to be lazy