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

How to detect if it is day, afternoon or night by a script?

Asked by 2 years ago

Hi everyone! I've working on a NPC that greet you with a GUI (Text label).

I want to make a text label that contain "Good morning" / "Good afternoon" / "Good evening".

Here is the local script:

local player = game.Players.LocalPlayer

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if game.Lighting.ClockTime >= 5 and game.Lighting.ClockTime >= 11 then

        script.Parent.Text = "Good morning, " .. player.Name .."! Welcome to the hotel! What can I do for you?"

    elseif game.Lighting.ClockTime >= 12 and game.Lighting.ClockTime >= 19 then

        script.Parent.Text = "Good afternoon, " .. player.Name .."! Welcome to the hotel! What can I do for you?"



    elseif game.Lighting.ClockTime >= 20 and game.Lighting.ClockTime >= 4 then
        script.Parent.Text = "Good evening, " .. player.Name .."! Welcome to the hotel! What can I do for you?"
    end
end)

But the NPC don't detect which hour it is...someone can say me which is the problem or what I should do to make it work?

Thanks!

0
How to detect if is it day, afternoon or night by a script?* Sorry for the writing ELEKTRO8415i 32 — 2y

1 answer

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

I think the problem is that you're checking if ClockTime is greater than and greater than, when it should be greater than and LESS than, something like this:

local player = game.Players.LocalPlayer

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
    if game.Lighting.ClockTime >= 5 and game.Lighting.ClockTime < 12 then

        script.Parent.Text = "Good morning, " .. player.Name .."! Welcome to the hotel! What can I do for you?"

    elseif game.Lighting.ClockTime >= 12 and game.Lighting.ClockTime < 20 then

        script.Parent.Text = "Good afternoon, " .. player.Name .."! Welcome to the hotel! What can I do for you?"



    elseif game.Lighting.ClockTime >= 20 and game.Lighting.ClockTime < 5 then
        script.Parent.Text = "Good evening, " .. player.Name .."! Welcome to the hotel! What can I do for you?"
    end
end)

Also, although ClockTime is ok to use, I would advice you to use TimeOfDay if you want to be more precise:

    if game.Lighting.TimeOfDay >= "6:00:00" and game.Lighting.TimeOfDay < "12:00:00" then --Use strings, otherwise it will error
--Your Code
Ad

Answer this question