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

Lighting ambience scripting issues (?)

Asked by 2 years ago

Hello everyone,

I'm trying to make a script that will change the ambience and brightness of in-game lighting depending on its ClockTime value but I seem to run into some issues.

This is what I have so far. The script is pretty big, because I want to make transitions smoother.

local ambience = game.Lighting.Ambient
local brightness = game.Lighting.Brightness

while true do

    if game.Lighting:GetMinutesAfterMidnight () > 17.8 * 60 then
        --game.LightingChanged.Ambient = Color3.fromRGB(154, 99, 15)
        print("it's 17:48")
        ambience = Color3.fromRGB(154, 99, 15)
        brightness = 4
    end
    if game.Lighting:GetMinutesAfterMidnight () > 19.3 * 60 then
        print("it's 19:18")
        ambience = Color3.fromRGB(112, 67, 0)
        brightness = 0
    end
    if game.Lighting:GetMinutesAfterMidnight () > 20.6 * 60 then
        print("it's 20:36")
        ambience = Color3.fromRGB(73, 44, 9)
        brightness = 0
    end
    if game.Lighting:GetMinutesAfterMidnight () > 5 * 60 then
        print("it's 05:00")
        brightness = 4
    end
    if game.Lighting:GetMinutesAfterMidnight () > 6.06 * 60 then
        print("it's 20:36")
        ambience = Color3.fromRGB(151, 110, 49)
        brightness = 4
    end
    if game.Lighting:GetMinutesAfterMidnight () > 6.4 * 60 then
        print("it's 20:36")
        ambience = Color3.fromRGB(179, 107, 0)
        brightness = 4
    end
    if game.Lighting:GetMinutesAfterMidnight () > 7 * 60 then
        print("it's 20:36")
        ambience = Color3.fromRGB(204, 132, 21)
        brightness = 4
    end
end

The output is giving out this error which im not sure what to do with it nor how to fix the issue " Script timeout: exhausted allowed execution time - Server - Ambience:4 "

If any of you could help me I will owe my life to you

2 answers

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago
while true do
end

This is an infinite loop that runs again right after finishing, it will run so fast that your code will just crash, your solution is to update ambience only when ClockTime property changes, in Roblox there is a function called GetPropertyChangedSignal, it returns you an event to which you can connect a function, this event will invoke your function every time the specified property changes. Your second issue in the code is that ambience and brightness values are numbers, in Lua numbers are copied every time you create a new variable, this means that

local a = 5
local b = a
b = 10
-- a: 5
-- b: 10
-- "a" and "b" are completely separate numbers

Doing ambience = 10 will only change the ambience variable and not game.Lighting.Ambient. To avoid this, you should not create a variable to these properties but just for the Lighting service. Also prefer using GetService over game.Service.

local Lighting = game:GetService("Lighting")

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
  if game.Lighting:GetMinutesAfterMidnight () > 17.8 * 60 then
    --game.LightingChanged.Ambient = Color3.fromRGB(154, 99, 15)
    print("it's 17:48")
    Lighting.Ambient = Color3.fromRGB(154, 99, 15)
    Lighting.Brightness = 4
  end
  if game.Lighting:GetMinutesAfterMidnight () > 19.3 * 60 then
    print("it's 19:18")
    Lighting.Ambient = Color3.fromRGB(112, 67, 0)
    Lighting.Brightness = 0
  end
  if game.Lighting:GetMinutesAfterMidnight () > 20.6 * 60 then
    print("it's 20:36")
    Lighting.Ambient = Color3.fromRGB(73, 44, 9)
    Lighting.Brightness = 0
  end
  if game.Lighting:GetMinutesAfterMidnight () > 5 * 60 then
    print("it's 05:00")
    Lighting.Brightness = 4
  end
  if game.Lighting:GetMinutesAfterMidnight () > 6.06 * 60 then
    print("it's 20:36")
    Lighting.Ambient = Color3.fromRGB(151, 110, 49)
    Lighting.Brightness = 4
  end
  if game.Lighting:GetMinutesAfterMidnight () > 6.4 * 60 then
    print("it's 20:36")
    Lighting.Ambient = Color3.fromRGB(179, 107, 0)
    Lighting.Brightness = 4
  end
  if game.Lighting:GetMinutesAfterMidnight () > 7 * 60 then
    print("it's 20:36")
    Lighting.Ambient = Color3.fromRGB(204, 132, 21)
    Lighting.Brightness = 4
  end
end)

Whenever you get better, try using tables to create a far shorter solution for this.

0
OMG thank you so much! This makes so such sense now! you saved my day ???????? DesignerPlastix 5 — 2y
1
yes i saved your whole life your kids will work for me for the rest of their days i own your DNA from now your whole family belongs to me imKirda 4491 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

You made a lot of mistakes you should never do. Well first of all why it crashes (yes, it crashes) is bc you made an infinity loop without any delays. The easy solution is to put wait(1) somewhere in a script. If you game changes time of the day not smoothly (i.e. it doesn't use TweenService or something to cycle trought the entire day without skipping any second) I higly recommend you to listen for a Lighting:GetPropertyChangedSignal("ClockTime") for an instant response:

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    -- Do your thing
end)

Second, you should use elseif/else in your script, otherwise it will look very weird when it applies changes to the ambience and then instantly changes it to something else. It prevents from multiple changes in the same time, do something like this:

if game.Lighting:GetMinutesAfterMidnight () > 20.6 * 60 then
    print("it's 20:36")
elseif game.Lighting:GetMinutesAfterMidnight () > 19.3 * 60 then
    print("it's 19:18")
elseif game.Lighting:GetMinutesAfterMidnight () > 17.8 * 60 then
    print("it's 17:48")
end

Important: check a time in a correct order (from highest to lowest) or else it will never go further after a first check like you did And the third mistake is that you are changin variables (on lines 9, 10, 14, 15...), not an ambience itself. To actually apply changes, do something like this:

local lighting = game.Lighting

if game.Lighting:GetMinutesAfterMidnight () > 20.6 * 60 then
    print("it's 20:36")
    lighting.Ambient = Color3.fromRGB(73, 44, 9)
    lighting.Brightness = 0
elseif game.Lighting:GetMinutesAfterMidnight () > 19.3 * 60 then
    print("it's 19:18")
    lighting.Ambient = Color3.fromRGB(112, 67, 0)
    lighting.Brightness = 0
elseif game.Lighting:GetMinutesAfterMidnight () > 17.8 * 60 then
    print("it's 17:48")
    lighting.Ambient = Color3.fromRGB(154, 99, 15)
    lighting.Brightness = 4
end

In the end you get:

local lighting = game.Lighting

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if game.Lighting:GetMinutesAfterMidnight () > 20.6 * 60 then
        print("it's 20:36")
        lighting.Ambient = Color3.fromRGB(73, 44, 9)
        lighting.Brightness = 0
    elseif game.Lighting:GetMinutesAfterMidnight () > 19.3 * 60 then
        print("it's 19:18")
        lighting.Ambient = Color3.fromRGB(112, 67, 0)
        lighting.Brightness = 0
    elseif game.Lighting:GetMinutesAfterMidnight () > 17.8 * 60 then
        print("it's 17:48")
        lighting.Ambient = Color3.fromRGB(154, 99, 15)
        lighting.Brightness = 4
    end
end)

Hope it's clear to understand. I actually had a few mistakes in the beggining of my scripting like yours, you just need to get used to it

0
yes this, performance efficient, better solution imKirda 4491 — 2y
0
Thank you very much for explaining everything to me! This truly means so much to my dumb little soul :') DesignerPlastix 5 — 2y
0
You should mark imKirda's or my answer as an answer to your question so no one will go here to try to answer your question when it's already answered MrSuperKrut 167 — 2y
0
You should mark imKirda's or my answer as an answer to your question so no one will go here to try to answer your question when it's already answered MrSuperKrut 167 — 2y

Answer this question