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

How can I make a script that selects several bricks to change their material?

function WorkingLightSwitch()
	local state = script.Parent.LightState
	local switchOn = script.Parent
	local switchOff = script.Parent.Parent.switchOff
	local neon = script.Parent.Parent.Neon
	if state.Value == false then
		state.Value = true
		neon:GetChildren().Material = "Neon"
		switchOn.Transparency = 0
		switchOff.Transparency = 1
	else
		state.Value = false
		neon:GetChildren().Material = "Plastic"
		switchOn.Transparency = 1
		switchOff.Transparency = 0
	end
end

script.Parent.ClickDetector.MouseClick:connect(WorkingLightSwitch)

It's a light switch, I have several neon blocks that I want to turn on and off once clicked. And they're in a model called Neon.

Apparently the switchOn and switchOff seem to work, but the neon:GetChildren() doesn't seem to work. Help?

1 comment

0

I think you use for i,v instead of get children.

1 answer

You have to actually iterate through the table returned from the GetChildren function using a generic for loop, and manipulate each index.

Also, you should define static variables outside of the function, so they aren't being defined everytime the function is called.

local state = script.Parent.LightState
local switchOn = script.Parent
local switchOff = script.Parent.Parent.switchOff
local neon = script.Parent.Parent.Neon

function WorkingLightSwitch()
	local bool = state.Value
	state.Value = not bool; --Replace if statement with logical operators.
	for _,v in next,neon:GetChildren() do --Generic for loop
		v.Material = bool and "Plastic" or "Neon";
	end
	switchOn.Transparency = bool and 1 or 0;
	switchOff.Transparency = bool and 0 or 1;
end

--Use Connect
script.Parent.ClickDetector.MouseClick:Connect(WorkingLightSwitch)

######note: connect is deprecated. Use Connect!

. . . . . . . . . . . . . . . . . . . . . .

##Operators

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