Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to change a property of all a model's children?

Asked by 8 years ago

I have an alarm system with 3 states, green, yellow and red. I need the color of a spotlight to change when one of the three buttons are pressed, but on green, the range to be 0, so it is off.

I have it set up like this:

Workspace

| Alarm (model)

| Alarm1 (model)

  | Spotlight

| Alarm 2

  | Spotlight

I need all of Alarm's children's spotlights to change, but how.

0
Well that attempt at formatting failed. Sharpthy 46 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago

You can use a generic for loop to loop through all the children in the alarm, check if the child in question is a SpotLight and change it's color if it is a SpotLight.

I would suggest making this in a function so you can call it for different alarms. If we use a function for this, we can also use the built-in assert function. Assert has two arguments: A boolean and a string. Assert checks if the boolean is false and throws an error into the output with the message of the string given.

This is useful as we can add a statement in place of a boolean in the assert function so that our function stops if a certain condition is not met and allows us to give custom error messages.


Code:

function changeAlarmLight(parent,r,g,b) --Parent is a model, r, g and b are values for use later on for changing the Color of the SpotLight.
    assert(parent:IsA("Model"), "ERROR: Parent is not a model!") --Asset checks if the first argument is false and throws an error into the output with a message from the second argument. In this case, we check if parent is a model and throw an error if it's not a model.
    assert(tonumber(r), "ERROR: Red value is nil or not a number!") --Checks if r is a number and throws an error if it isn't.
    assert(tonumber(g), "ERROR: Green value is nil not a number!")
    assert(tonumber(b), "ERROR: Blue value is nil or not a number!")
    if r > 1 then r = r/255 end --If the r value is over 1, we assume it's part of the 255 scale and divide by 255 respectively.
    if g > 1 then g = g/255 end
    if b > 1 then b = b/255 end
    for _,light in pairs(parent:GetChildren()) do --Gets all the children of the parent specified.
        if light:IsA("SpotLight") then --Checks if the child currently being looped through is a SpotLight.
            light.Color = Color3.new(r,g,b) --Changes SpotLight color based on the r, g and b values.
        end
    end
end

Usage example:

changeAlarmLight(game.Workspace.Alarm,255,0,0) --Changes SpotLight in Alarm to the color red.
changeAlarmLight(game.Workspace.Part,255,100,20) --Prints "ERROR: Parent is not a model!"
changeAlarmLight(game.Workspace.Alarm1,true,0,0) --Prints "ERROR: Red value is nil or not a number!"
--And so on...

I hope my answer helped you. If it did, be sure to accept it.

Read more on the asset function here.

Ad

Answer this question