GetChildren() Function Only Selects One Child

Hey everybody, I am very confused. I am writing code controlling a fire alarm system in my game. I made a for loop cycling through the workspace, finding the child named, “Alarm.” Every alarm has the same children, and I am looking for the pointlight, which is supposed to flash on, and off. The problem is that the script only chooses 1 not all the children under that name. I have tried making a value inside each alarm, and putting an if statement to see if the value is in there. I don’t know what to do about this. If you need me to clear up the situation, because it is kinda confusing, please let me know. Thanks!

game.Players.PlayerAdded:Connect(function()
	while wait() do
		if game.Workspace:FindFirstChild("Fire") then
			for _,v in pairs(game.Workspace:GetChildren()) do
				if v.Name == "Alarm" and v:IsA("Model") and v:FindFirstChild("alarm") then --v:FindFirstChild("alarm") is for finding that value inside of it for identification.
					while wait() do
						game.Workspace["Realistic Fire Alarm sound"]:Play()
						v:FindFirstChild("Strobe"):FindFirstChild("Light").Enabled = true
						v:FindFirstChild("Strobe"):FindFirstChild("Flasher").Enabled = true
						wait(1)
						v:FindFirstChild("Strobe"):FindFirstChild("Light").Enabled = false
						v:FindFirstChild("Strobe"):FindFirstChild("Flasher").Enabled = false
						wait(1)
					end
				end
			end
		end
	end
end)

I’m assuming your script is running on the server? If that is the case, you are essentially running this code multiple times as when a player joins, this code gets executed again.

Yes, it is running on the server. Maybe should I remove the player added function?

He’s also while looping in a for loop, which appears to go on forever. So infinite loops every time a player joins? Not smart.

1 Like

Yes that would be smart choice. You only need server code to run once. Also take into account what @MichaelEpicA said about your while loop embedded inside your for loop.

You are stuck on a while loop on the first for loop, that’s why the loop is not progressing. Instead, you should use task.spawn() to make sure the for loop progresses.

task.spawn(function()
while task.wait() do

end
end)

2 Likes

Wow. Thanks for letting me know. I will fix that now. I should have reviewed the code better. Do you mind adding an example to the whole code above?

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