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

Day/Night building Material does not change?

Asked by 5 years ago
Edited by SerpentineKing 5 years ago

This is the script here

building = script.Parent
if game.Lighting.ClockTime >= script.Parent.Night.Value then
    wait(1)
    building.Material = "Neon"
elseif game.Lighting.ClockTime >= script.Parent.Day.Value then
    building.Material = "Plastic"
end

I try to make it so it will make the building neon if it's = to the number value I put (ex. 19) but it's not working when the day or the night comes

[SerpentineKing]: Input proper code format

0
hmmm clean it please? starmaq 1290 — 5y
0
You put it incorrectly.. wilsonsilva007 373 — 5y
0
I try to make it correct XD doesn't work bostaffmanbulgaria1 89 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

GENERAL PRACTICE

Use :GetService() to retrieve Lighting

Use :WaitForChild() to make sure an item exists before changing it.

Make sure to use local variables when a line, like "building" is meant to be inside a function that is not global.

Material is an Enum, not a String, so while the string may work some of the time, it is not always guaranteed.

Since you are checking that the ClockTime is greater than the day and night values, also make sure to have a check that the time is less than the night so the "day" change can run.

ISSUES

Your code needs to be placed within a function to work multiple times with the same activating requirements. Without the function, the code will only run once at the time the server is created. In the below example, I've used a :GetPropertyChangedSignal() to determine that the ClockTime of the Lighting is changed (as opposed to Brightness or another property).

REVISED SERVER SCRIPT

local lighting = game:GetService("Lighting")
local building = script.Parent
local night = building:WaitForChild("Night").Value
local day = building:WaitForChild("Day").Value

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if lighting.ClockTime >= night then
        wait(1)
        building.Material = Enum.Material.Neon
    elseif lighting.ClockTime >= day and lighting.ClockTime < night then
        building.Material = Enum.Material.Plastic
    end
end)
Ad

Answer this question