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

Script Doesnt Change :GetChildren() Parents?

Asked by 2 years ago

im making a script that theres 2 folders in replicatedStorage, both having settings for daytime and nighttime and the script will get the children from one of the folders and move the other children back into their folders, playing music with the theme/time, but it doesn't work, I'm suspecting that I'm using the wrong function and supposed to use something else instead of :GetChildren() but i don't know what it is, i would appreciate any help, I'm not very good at coding, the script is in workspace btw

01local Lighting = game:GetService("Lighting")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03local daySettings = ReplicatedStorage.Daytime:GetChildren()
04local nightSettings = ReplicatedStorage.Nighttime:GetChildren()
05local LightingChildren = game:GetService("Lighting"):GetChildren()
06 
07while true do
08LightingChildren.Parent = nightSettings
09daySettings.Parent = Lighting
10script.Day:Play()
11    wait(378.346)
12    script.Day:Stop()
13    wait(1)
14    LightingChildren.Parent = daySettings
15    nightSettings.Parent = Lighting
16    script.Night:Play()
17    wait(257.291)
18    script.Night:Stop()
19    wait(1)
20    end

2 answers

Log in to vote
2
Answered by 2 years ago
Edited 2 years ago

Expanding on Kingu_Criminal's answer.

As Kingu_Criminal said, the GetChildren method returns an array. In the script you provided, you attempt to modify the table rather than the objects inside it. You must index the array with a number to get the value from the table.

For example

1local Table = {"apple", 1}
2 
3print(Table) -- would print the actual table object
4 
5print(Table[1]) -- is indexing the first element of the table, which would be "apple"
6print(Table[2]) -- is indexing the second element of the table, which is the number 1

Just like Kingu_Criminal said, you can use a for loop to iterate over the table

01local Table = {"apple", 1, 8, true, false}
02 
03local currentIteration = 1
04for index, value in ipairs(Table) do
05    print("Iteration #"..currentIteration.." = Index Position:", index.." | " .."Value:", value)
06    currentIteration += 1  
07end
08 
09Output = [[
10Iteration #1 = Index Position: 1 | Value: apple
11Iteration #2 = Index Position: 2 | Value: 1
12Iteration #3 = Index Position: 3 | Value: 8
13Iteration #4 = Index Position: 4 | Value: true
14Iteration #5 = Index Position: 5 | Value: false
15]]

The for loop will iterate over the array and return the index position and the value for the current iteration.

01local Lighting = game:GetService("Lighting")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03local daySettings = ReplicatedStorage.Daytime:GetChildren()
04local nightSettings = ReplicatedStorage.Nighttime:GetChildren()
05local LightingChildren = game:GetService("Lighting"):GetChildren()
06 
07local function setParent(children, parent) -- Using a function is just for convenience so you wouldn't have to use multiple for loops to do the same thing.
08    for _, child in ipairs(children) do -- The underscore means we won't be using the index position.
09        child.Parent = parent
10    end
11end
12 
13while true do
14 
15    setParent(LightingChildren, ReplicatedStorage.Nighttime)
View all 30 lines...

Let me know if you need any further assistance.

Ad
Log in to vote
0
Answered by 2 years ago

GetChildren returns an array {}. It will return multiple values, if there is a specific child you want you have to index it directly or loop through the children with a for i, v in ipairs() do end loop

0
sorry but, i dont get this answer JmoneyPlayzOfficial 49 — 2y

Answer this question