How can I apply one script to multiple models?

Yep. (30charsssssssssssssssss)

Okay so it doesnā€™t give me an error but it doesnā€™t work either. I tried @downcrusher690ā€™s code but it kept cloning the scripts, saturating the models and giving me an error. How can I prevent that anyways?

Try this script(Edit from T_eethyz):

for _, Lamp in pairs(workspace:WaitForChild("Lamps"):GetChildren()) 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
1 Like

It results in one lamp broken and the other ignoring the script

Oh yeah forgot about the while loop. You should separate the

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)
    wait()
end

into a different script because this while loop is causing the script not moving on to the next lamp. But also put the rest of the code inside another while loop so that it can check for the time forever.

1 Like

Same results as before :confused:

Iā€™d recommend using the CollectionService and tagging all the lamp parts with a ā€˜Lampā€™ tag.
This will easily allow you modify multiple parts with your single script, no cloning required.

Simply get this plugin, create a tag, and add it to each part you want to change.

image

Hereā€™s a code snippet you can use to replace your original.

local CollectionService = game:GetService("CollectionService")

if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then --Between 6am and 6pm (day)		
    for _, light in ipairs(CollectionService:GetTagged("Lamp")) do --Interate through every object parented to the model
       light.Color = lightDayColor --Set color
       light.Material = Enum.Material.SmoothPlastic --Set material
       light[pointLightName].Enabled = false --Disable light
    end
else --Between 6pm and 6am (night)
     for _, light in ipairs(CollectionService:GetTagged("Lamp")) do --Interate through every object parented to the model
        light.Color = lightDayColor --Set color
        light.Material = Enum.Material.Neon --Set Material
        light[pointLightName].Enabled = true --Enable light
    end
end
6 Likes

Oh my god, I am really sorry I had made a small mistake.
Iā€™ve corrected the code if you are still looking for any help you may contact me whenever.

If youā€™re looking for an even more advanced and easier solution Iā€™d suggest what @Deadman7117 has replied with.

1 Like

Iā€™m really interested in your script but I need help configuring it so it works propertly. I did the change of adding the :GetChildren() right after the WaitForChild(ā€œLampsā€) and yet it doesnā€™t quite get it fully since thereā€™s a constant loop inside the while loop that prevents it from ending. Iā€™ve been suggested to separate said looping part by @FlashFlame_Roblox without having any different results.

I seem to have confused you so I am going to just post the code itself.

local minutesAfterMidnight = 60*6
local daySpeed = 1
local dayTime = 6
local nightTime = 18
local lightName = "Light"
local pointLightName = "PointLight"
local lightDayColor = Color3.fromRGB(160, 160, 160)
local lightNightColor = Color3.fromRGB(255, 255, 255)

local lighting = game:GetService("Lighting")

for _, Lamp in next, workspace:WaitForChild("Where all the Lamps are stored"):GetChildren()
    while true do
        minutesAfterMidnight = minutesAfterMidnight + (daySpeed)
        if minutesAfterMidnight > 60*24 then
            minutesAfterMidnight = 0
        end
        
        lighting:SetMinutesAfterMidnight(minutesAfterMidnight)
        
        if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then
            for i, light in pairs(Lamp:GetChildren()) do
                if light.Name==lightName then
                    light.Color = lightDayColor
                    light.Material = Enum.Material.SmoothPlastic
                    light[pointLightName].Enabled = false
                end
            end
        else --Between 6pm and 6am (night)
            for i, light in pairs(Lamp:GetChildren()) do
                if light.Name==lightName then
                    light.Color = lightDayColor
                    light.Material = Enum.Material.Neon
                    light[pointLightName].Enabled = true 
                end
            end
        end
        
        wait()
    end
end
1 Like

This is so weirdā€¦

The code tags it by itself, right? How do I attach the code to the tag? Or is it automatic? I tried the plugin but it doesnā€™t seem to work or Iā€™m using it wrong

When you select an object in the explorer, the tag window will show what tags it has applied to it. To add a tag to the parts, select the tag in the list. (make sure you actually create the tag)

image

1 Like

Use collection service, tag the model ā€œLampā€ or something, then use a script to loop through all tagged models to do stuff to it:

local CS = game:GetService("CollectionService")

function turnOn()  
    for i, model in pairs(CS:GetTagged("Lamp")) do --loops through every tagged model
        --code to turn on light       
    end
end

function turnOff()
    for i, model in pairs(CS:GetTagged("Lamp")) do
        --code to turn off light
    end
end

while true do --there's probably a better way to do this
    if minutesAfterMidnight >= 60 * dayTime and minutesAfterMidnight < 60 * nightTime then
        turnOff()
    else
        turnOn()
    end
    wait(however long you want) --so it doesn't fire a million times a sec
end

Boom, now no need to clone scripts and lag up your whole game. :slight_smile:

idk if this will work, im totally not qualified to be explaining this kind of stuff

5 Likes

Thanks to @downcrusher69ā€™s script, with some teaks made by him, Iā€™ve been able to apply only one script per model. For future references and everyone who has run into this problem, this is a quick and simple solution.

cloningScri.lua (303 Bytes)

For it to work properly, it has to be a separate script with the proper configuration of your own.

Thank you for eveyone that participated in the post. @T_eethyz code looked promicing, Iā€™ll definetly look further into it. As well as the Tag Editor plugin.

1 Like