How do i pick a random model and the part within it?

I want to make my lights/indicators light up randomly. So far this is my script:

local items = script.Parent.Folder:GetChildren()
local randomItem = items[math.random(1,#items)]
while true do
for _,v in pairs (randomItem:GetChildren()) do
	if v:IsA("Model") and v.Name == "Indicator" then
		v.Ind.Material = Enum.Material.Neon
		task.wait(.5)
		v.Ind.Material = Enum.Material.SmoothPlastic
	end	
	end
	task.wait()
	end

I’ve tried countless methods but to no avail. Can somebody tell me why it doesn’t work?

3 Likes

Currently you pick a random item once, then when the loop runs this random item never changes.
You need to place the random item declaration line inside the loop.
(I’m assuming the paths are correct for the parts)

local items = script.Parent:GetChildren()

while true do
local randomItem = items[math.random(1,#items)]
for _,v in pairs (randomItem:GetChildren()) do
	if v:IsA("Model") and v.Name == "Indicator" then
		v.Ind.Material = Enum.Material.Neon
		task.wait(.5)
		v.Ind.Material = Enum.Material.SmoothPlastic
	end	
	end
	task.wait()
	end
1 Like

Yeah, i’ve tried that but the indicators don’t change their materials at all. It’s not even printing anything out, even if i tell it to do so…

1 Like

what is your script classname and where did you parent the script

1 Like

Sorry for the late reply but the models are inside a folder and the script is outside that folder parented to a model and also it’s a serverscript

1 Like

NEVERMIND I FIGURED IT OUT

local items = workspace.RandomIndicators.Folder:GetChildren()
while task.wait(2) do
local randomItem = items[math.random(1,#items)]
	print(randomItem)
	for _,v in pairs(randomItem:GetChildren()) do
			print("loop")
					v.Ind.Material = Enum.Material.Neon
					task.wait(0.5)  -- Changed from task.wait() to wait()
		v.Ind.Material = Enum.Material.SmoothPlastic
					end	
					end

WOOO IT WORKS CORRECTLY NOW!

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