Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make the Roblox Tutorial LightPart script work on multiple parts?

I used the LightPart scripot from the roblox tutorial but I want the script to work on multiple parts cause it only works on one Part called Lightpart Not multiple parts called LightPart.

lightPart = game.Workspace.LightPart minutesAfterMidnight = 0

while true do minutesAfterMidnight = minutesAfterMidnight + 10 game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight) wait(0.1)

if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then		-- checks for 6AM
	lightPart.Material = Enum.Material.Plastic
	lightPart.PointLight.Enabled = false
end
if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then		-- checks for 6PM
	lightPart.Material = Enum.Material.Neon
	lightPart.PointLight.Enabled = true
end

end

1 answer

The best way to do this is using the GetChildren function.

GetChildren will return a table, which you can loop through with a generic for loop, then for each part, check to see if it is called LightPart, and if so update the part.

You can implement it like this:

minutesAfterMidnight = 0
 
while true do
	minutesAfterMidnight = minutesAfterMidnight + 10
	game.Lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
	wait(0.1)
 
	if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then		-- checks for 6AM
		for k, v in pairs(game.Workspace:GetChildren()) do
			if v.Name == "LightPart" then
				lightPart.Material = Enum.Material.Plastic
				lightPart.PointLight.Enabled = false
			end
		end
	end
	if game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then		-- checks for 6PM
		for k, v in pairs(game.Workspace:GetChildren()) do
			if v.Name == "LightPart" then
				lightPart.Material = Enum.Material.Neon
				lightPart.PointLight.Enabled = true
			end
		end
	end
end

1 comment

0

In the for loop shouldn't it be "v.Material = Enum.Material.Plastic" "v.Pointlight.Enabled = false"?

This discussion is preserved as it was. New answers, comments, and votes are closed.