--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.
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.
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
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
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.
@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
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.
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).