How can I apply one script to multiple models?

I have an imported lighting system script:

Light system script
--Script in Model containing lights
local minutesAfterMidnight = 60*6 --Starts at 6 am
local daySpeed = 1 --Configure this to make days go shorter or faster (2 = twice as fast, 0.5 = half as fast)
local dayTime = 6 --Number of hours after midnight that it becomes day
local nightTime = 18 --Number of hours after midnight that it become night
local lightName = "Light" --Name of parts to store light in
local pointLightName = "PointLight" --Name of point light or other light object with .Enabled property
local lightDayColor = Color3.fromRGB(160, 160, 160) --Color to make light when day time
local lightNightColor = Color3.fromRGB(255, 255, 255) --Color to make light when night time

local lighting = game:GetService("Lighting") --Lighting service
local model = script.Parent --Where are lights are stored
while true do
	minutesAfterMidnight = minutesAfterMidnight + (daySpeed) --Increments counter based on daySpeed
	if minutesAfterMidnight > 60*24 then --Resets counter at midnight
		minutesAfterMidnight = 0
	end
	
	lighting:SetMinutesAfterMidnight(minutesAfterMidnight) --Sets time
	
	if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then --Between 6am and 6pm (day)
		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if light.Name==lightName then --It is a light and not just another part
				light.Color = lightDayColor --Set color
				light.Material = Enum.Material.SmoothPlastic --Set material
				light[pointLightName].Enabled = false --Disable light
			end
		end
	else --Between 6pm and 6am (night)
		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if light.Name==lightName then --It is a light and not just another part
				light.Color = lightDayColor --Set color
				light.Material = Enum.Material.Neon --Set Material
				light[pointLightName].Enabled = true --Enable light
			end
		end
	end
	
	wait()
end

For a model but this model repeats itself multiple times in the map so I wondered if there is a way to set the script once and make it so it applies to the rest of the same models.

Thank you in advance.

7 Likes

You can store the script in one place, such as ServerStorage or ServerScriptService, and then use a for loop to clone the script and insert it into the models

for i,v in pairs(game.Workspace:GetChildren()) do
    if v.Name == "LightingModelOrWhatever" then
        local newScript = script:Clone() --make sure this refers to the script itself
        newScript.Parent = v
    end
end

This way, you can just edit one script and then those changes will be applied at runtime when the script gets copied into the models.

5 Likes

Adding on to what @downcrusher69 said, you should run the script in the command bar. That way you won’t have to playtest for the script to run. Just copy the script into the command bar and click enter.
View>command bar

1 Like

So instead of script it would be the name of the script, right?

It also produces my game to lag constantly while giving the error:
startScript re-entrancy has exceeded 3

You can make a folder inside of workspace or where ever you prefer which contains all the models and then instead of applying all the changes to, for example: script.Parent you can instead apply them to all the children inside of the folder.

This would save time and effort and it would also automatically apply the script to newer models inside of the Folder.

Example:

for _, Lamp in next, workspace:WaitForChild("Lamps"):GetChildren() do
    local model = Lamp
    -- Rest of the code.
end
2 Likes

That’s actually a great idea! What I also noticed it that it clones the script to the models but does it seeveral times to the point where there are multiple clones of the code inside the model. How can I solve this?

What do you mean? You aren’t supposed to clone the script, instead you are supposed to have one script inside of some where such as ServerScriptService or workspace where it would handle all of your models from one place without spamming scripts unnecessarily.

1 Like

I would suggest doing

for i,v in pairs(workspace:GetDescendants) do
    if v.Name ~= "$ModelNameHere" then return end
    NewScript = script:Clone()
    NewScript.Parent = v
end

Either in the command line or on the very top of the lighting script.

1 Like

Inside the while loop or in the first line of the code?

First line of the script, the code just replicates itself to all models that have the same name as $ModelNameHere.

1 Like

What I gather with this is that the for loop will contain all the local variables and the while loop as well, right?

You’re just going to have to use the script that you used before but instead of using script.Parent you can add Lamp.

1 Like

I just tested it and the error that displays on my command bar says:

ServerScriptService.LightSystem:1: bad argument #1 (table expected, got Object)

Meaning that it’s passing Lamp as a table and not single objects

@T_eethyz’s way is different from mine.
He is taking all models in a given folder or with a given name and applying the script to those models.
My way clones the script and puts them in the models with a certain name.

In summary, you can do it any way you want. I wasn’t aware that T_eethyz already answered. But his way is more elegant so I’d suggest you use his. (Creates less of a mess)

Code for T_eethyz's Way
for _, Lamp in next, workspace:WaitForChild("Lamps") do
    local model = Lamp
    --Script in Model containing lights
    local minutesAfterMidnight = 60*6 --Starts at 6 am
    local daySpeed = 1 --Configure this to make days go shorter or faster (2 = twice as fast, 0.5 = half as fast)
    local dayTime = 6 --Number of hours after midnight that it becomes day
    local nightTime = 18 --Number of hours after midnight that it become night
    local lightName = "Light" --Name of parts to store light in
    local pointLightName = "PointLight" --Name of point light or other light object with .Enabled property
    local lightDayColor = Color3.fromRGB(160, 160, 160) --Color to make light when day time
    local lightNightColor = Color3.fromRGB(255, 255, 255) --Color to make light when night time

    local lighting = game:GetService("Lighting") --Lighting service
    while true do
    	minutesAfterMidnight = minutesAfterMidnight + (daySpeed) --Increments counter based on daySpeed
    	if minutesAfterMidnight > 60*24 then --Resets counter at midnight
    		minutesAfterMidnight = 0
    	end
    	
    	lighting:SetMinutesAfterMidnight(minutesAfterMidnight) --Sets time
    	
    	if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then --Between 6am and 6pm (day)
    		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
    			if light.Name==lightName then --It is a light and not just another part
    				light.Color = lightDayColor --Set color
    				light.Material = Enum.Material.SmoothPlastic --Set material
    				light[pointLightName].Enabled = false --Disable light
    			end
    		end
    	else --Between 6pm and 6am (night)
    		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
    			if light.Name==lightName then --It is a light and not just another part
    				light.Color = lightDayColor --Set color
    				light.Material = Enum.Material.Neon --Set Material
    				light[pointLightName].Enabled = true --Enable light
    			end
    		end
    	end
    	
    	wait()
    end
end
Code for my way
for i,v in pairs(workspace:GetDescendants) do
    if v.Name ~= "$ModelNameHere" then return end
    NewScript = script:Clone()
    NewScript.Parent = v
end
--Script in Model containing lights
local minutesAfterMidnight = 60*6 --Starts at 6 am
local daySpeed = 1 --Configure this to make days go shorter or faster (2 = twice as fast, 0.5 = half as fast)
local dayTime = 6 --Number of hours after midnight that it becomes day
local nightTime = 18 --Number of hours after midnight that it become night
local lightName = "Light" --Name of parts to store light in
local pointLightName = "PointLight" --Name of point light or other light object with .Enabled property
local lightDayColor = Color3.fromRGB(160, 160, 160) --Color to make light when day time
local lightNightColor = Color3.fromRGB(255, 255, 255) --Color to make light when night time

local lighting = game:GetService("Lighting") --Lighting service
local model = script.Parent --Where are lights are stored
while true do
	minutesAfterMidnight = minutesAfterMidnight + (daySpeed) --Increments counter based on daySpeed
	if minutesAfterMidnight > 60*24 then --Resets counter at midnight
		minutesAfterMidnight = 0
	end
	
	lighting:SetMinutesAfterMidnight(minutesAfterMidnight) --Sets time
	
	if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then --Between 6am and 6pm (day)
		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if light.Name==lightName then --It is a light and not just another part
				light.Color = lightDayColor --Set color
				light.Material = Enum.Material.SmoothPlastic --Set material
				light[pointLightName].Enabled = false --Disable light
			end
		end
	else --Between 6pm and 6am (night)
		for i, light in pairs(model:GetChildren()) do --Interate through every object parented to the model
			if light.Name==lightName then --It is a light and not just another part
				light.Color = lightDayColor --Set color
				light.Material = Enum.Material.Neon --Set Material
				light[pointLightName].Enabled = true --Enable light
			end
		end
	end
	
	wait()
end
1 Like

I welcome every approach to the solution and I try to understand each and every one of them. More ways to solve the problem means more learning progress and future references for future problems as well. Thank you kindly.

Then again, @T_eethyz’s way has a problem. It returns the error:

ServerScriptService.LightSystem:1: bad argument #1 (table expected, got Object)

So I don’t know if this could be fixeable cuz it will always return Lamp as a table

That’s because it is getting only one part named Lamps(or model idk). @downcrusher69’s way is guaranteed to get all the stuffs named Lamps(or if Lamps is a model then just add a GetChildren to the t_eethyz script).

1 Like

Where do I have to add the GetChildren function?

Add it right after the workspace:WaitForChild("Lamps") and it will return all the parts in that model as a table.

1 Like

But Lamps is not a model. It’s a folder. Should it still work?