DisplaySystemMessage won't work

This is my first time working with DisplaySystemMessage() but I seemingly can’t get it to post anything in chat. Might anyone be able to tell me why based on the code and hierarchy?

task.wait(2)
local Lighting = game:GetService("Lighting")
local TextChatService = game:GetService("TextChatService")

local tag = "[Coalopolis]:"

function sunsetWarning()
	TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(tag .. "The sun is starting to set!")
end

function sunriseWarning()
	TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(tag .. "The sun is starting to rise!")
end

if Lighting.ClockTime == 6 then
	sunriseWarning()
end

The issue is most likely the if Lighting.ClockTime == 6 line. It may not be exactly 6 if the time is being animated.

Try this instead!

local lastTime = Lighting.ClockTime
RunService.Heartbeat:Connect(function()
    local time = Lighting.ClockTime

    if time >= 6 and lastTime < 6 then
        sunriseWarning()
    end

    lastTime = time
end)

Let me know if you have any questions.

syntax error

1 Like

Your method worked! I only have one question, being what exactly made it more effective then the way I was originally trying to implement it?

Your code was only checking the ClockTime once, meaning if it wasn’t passed 6 then it would never print, even if the time did eventually pass 6. My code checks every heartbeat (60 times a second) to see if the ClockTime has just passed 6!

Thanks a bunch for elaborating, and giving me the fix to my problem :smiley:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.