GetDescendants() not working

robloxapp-20240808-1740593.wmv (3.8 MB)
I have a light switch script and it works kinda but it doesn’t get all of the lights if you look at the video.

Script

local part = script.Parent.Parent.Parent.SwitchPart
local pos = part.Position
local ori = part.Orientation
local size = part.Size
local tweenService = game:GetService("TweenService")
local switchpart2 = script.Parent.Parent.Parent.SwitchPart2
local lights = false

local changes = {
	Position = switchpart2.Position;
	Orientation = switchpart2.Orientation
	--Color = Color3.new(0, 0, 0)
	--Size = size + Vector3.new(0, 0, 0)
}

local changes2 = {
	Position = part.Position;
	Orientation = part.Orientation
	--Color = Color3.new(0, 0, 0)
	--Size = size + Vector3.new(0, 0, 0)
}

local tweenInfo = TweenInfo.new(
	1,									-- Time taken for a full animation
	Enum.EasingStyle.Sine,			-- Animation Style
	Enum.EasingDirection.InOut,			-- Animation Type
	0,									-- Number of repeats (-1 is infinite)
	false,								-- Reverse?
	1									-- Delay between animations
)

local tween = tweenService:Create(part, tweenInfo, changes)
local tween2 = tweenService:Create(part, tweenInfo, changes2)
local lvlights = game.Workspace.LivingRoom.LivingRoomLights
script.Parent.Triggered:Connect(function()
	for _, v in pairs(lvlights:GetDescendants()) do
		if v:IsA("SpotLight") then
			if not lights then
				lights = true
				tween:Play()
				v.Enabled = true
				v.Parent.Material = Enum.Material.Neon
				v.Parent.BrickColor = BrickColor.new(237, 234, 234)
			else
				lights = false
				tween2:Play()
				v.Enabled = false
				v.Parent.Material = Enum.Material.Plastic
				v.Parent.BrickColor = BrickColor.new(0, 0, 0)
			end
		end
	end
end)

I’m guessing this is server script? I say you should yield the script until the game fully loads like task.wait(1) because it cannot load in certain objects in time. Let me know if that works.

Nope, it’s like there is at least 4 lights still on.

Are you sure they all are spotlights and in the same folder? Can you print(lights) and print(v) under v:IsA("Spotlight")?

  17:59:04.166  SpotLight  -  Server - Tween:42
  17:59:05.183  false  -  Server - Tween:41
  17:59:05.183  SpotLight  -  Server - Tween:42
  17:59:05.187  true  -  Server - Tween:41
  17:59:05.187  SpotLight  -  Server - Tween:42
  17:59:06.201  false  -  Server - Tween:41
  17:59:06.201  SpotLight  -  Server - Tween:42
  17:59:06.204  true  -  Server - Tween:41
  17:59:06.204  SpotLight  -  Server - Tween:42
  17:59:07.216  false  -  Server - Tween:41
  17:59:07.216  SpotLight  -  Server - Tween:42
  17:59:07.221  true  -  Server - Tween:41
  17:59:07.221  SpotLight  -  Server - Tween:42
  17:59:08.232  false  -  Server - Tween:41
  17:59:08.232  SpotLight  -  Server - Tween:42
  17:59:08.236  true  -  Server - Tween:41
  17:59:08.236  SpotLight  -  Server - Tween:42
  17:59:09.250  false  -  Server - Tween:41
  17:59:09.250  SpotLight  -  Server - Tween:42

So it keeps flashing true and false even though I flicked the switch once

I fixed it!

script.Parent.Triggered:Connect(function()
	if not lights then
		for _, v in pairs(lvlights:GetDescendants()) do
			if v:IsA("SpotLight") then
				lights = true
				tween:Play()
				v.Enabled = true
				v.Parent.Material = Enum.Material.Neon
				v.Parent.BrickColor = BrickColor.new(237, 234, 234)
				rs.Values.LightsTurnedOff.Value -= 1
			end
		end	
	else
		for _, v in pairs(lvlights:GetDescendants()) do
			if v:IsA("SpotLight") then
				lights = false
				tween2:Play()
				v.Enabled = false
				v.Parent.Material = Enum.Material.Plastic
				v.Parent.BrickColor = BrickColor.new(0, 0, 0)
				rs.Values.LightsTurnedOff.Value += 1
			end
		end	
	end
end)

I put the code in the wrong formation before.

The general implementation, while functional, is inefficient.

-- Spotlight cache
local spotlights = {}
for _, v in pairs(lvlights:GetDescendants()) do
    if v:IsA("SpotLight") then
        table.insert(spotlights, v)    
    end
end

local function updateLightsState(state)
    local material = state and Enum.Material.Neon or Enum.Material.Plastic
    local color = state and BrickColor.new(237, 234, 234) or BrickColor.new(0, 0, 0)
    local stateval = enable and -1 or 1
    if state then
       tween:Play()
    else
       tween2:Play()
    end
    
    for _, v in pairs(spotlights) do
        v.Parent.Material = material
        v.Parent.BrickColor = color
        v.Enable = state
    end
    rs.Values.LightsTurnedOff.Value = rs.Values.LightsTurnedOff.Value + stateval
end

script.Parent.Triggered:Connect(function()
    lights = not lights
    updateLightsState(lights)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.