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
local Lighting = game:GetService("Lighting") local ReplicatedStorage = game:GetService("ReplicatedStorage") local daySettings = ReplicatedStorage.Daytime:GetChildren() local nightSettings = ReplicatedStorage.Nighttime:GetChildren() local LightingChildren = game:GetService("Lighting"):GetChildren() while true do LightingChildren.Parent = nightSettings daySettings.Parent = Lighting script.Day:Play() wait(378.346) script.Day:Stop() wait(1) LightingChildren.Parent = daySettings nightSettings.Parent = Lighting script.Night:Play() wait(257.291) script.Night:Stop() wait(1) end
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
local Table = {"apple", 1} print(Table) -- would print the actual table object print(Table[1]) -- is indexing the first element of the table, which would be "apple" print(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
local Table = {"apple", 1, 8, true, false} local currentIteration = 1 for index, value in ipairs(Table) do print("Iteration #"..currentIteration.." = Index Position:", index.." | " .."Value:", value) currentIteration += 1 end Output = [[ Iteration #1 = Index Position: 1 | Value: apple Iteration #2 = Index Position: 2 | Value: 1 Iteration #3 = Index Position: 3 | Value: 8 Iteration #4 = Index Position: 4 | Value: true Iteration #5 = Index Position: 5 | Value: false ]]
The for loop will iterate over the array and return the index position and the value for the current iteration.
local Lighting = game:GetService("Lighting") local ReplicatedStorage = game:GetService("ReplicatedStorage") local daySettings = ReplicatedStorage.Daytime:GetChildren() local nightSettings = ReplicatedStorage.Nighttime:GetChildren() local LightingChildren = game:GetService("Lighting"):GetChildren() local 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. for _, child in ipairs(children) do -- The underscore means we won't be using the index position. child.Parent = parent end end while true do setParent(LightingChildren, ReplicatedStorage.Nighttime) setParent(daySettings, Lighting) script.Day:Play() task.wait(378.346) script.Day:Stop() task.wait(1) setParent(LightingChildren, ReplicatedStorage.Daytime) setParent(nightSettings, Lighting) script.Night:Play() task.wait(257.291) script.Night:Stop() task.wait(1) end
Let me know if you need any further assistance.
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