Hey guys, I’ve run into another kink while working on my day and night system. Currently I have it so that once a certain time of day is reached, the game will send out a message in chat as one way to notify the player. However, at the moment it appears to be sending twice instead of just once. If anyone can please look over my code real quick to see if there’s any blatant issues related to this I’ll greatly appreciate it
local dayNightWarningsModule = {}
local TextChatService = game:GetService("TextChatService")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local globalVariables = require(ReplicatedStorage.Modules.Data.globalVariables)
local lockTime = ReplicatedStorage.Values.BoolValues.lockTime
local currentTimeofDay = ReplicatedStorage.Values.StringValues.currentTimeofDay
local inBattle = ReplicatedStorage.Values.BoolValues.inBattle
local inTrade = ReplicatedStorage.Values.BoolValues.inTrade
local dayNightGong = ReplicatedStorage.Sounds.Overworld.dayNightGong
local prefix = "[Coalopolis]: "
--Time warnings and announcements
local function morningAnnouncement()
dayNightGong:Play()
TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(prefix.."Morning has risen over the city!")
end
local function nightAnnouncement()
dayNightGong:Play()
TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(prefix.."Night has fallen over the city!")
end
local function sunriseWarning()
TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(prefix.."The sun is rising.")
end
local function sunsetWarning()
TextChatService.TextChannels.RBXSystem:DisplaySystemMessage(prefix.."The sun is setting.")
end
--Send warnings
function dayNightWarningsModule.sendWarning()
if (currentTimeofDay.Value == "Morning" and lockTime.Value == false and inBattle.Value == false and inTrade.Value == false) then
morningAnnouncement()
elseif (currentTimeofDay.Value == "Night" and lockTime.Value == false and inBattle.Value == false and inTrade.Value == false) then
nightAnnouncement()
elseif (currentTimeofDay.Value == "Dawn" and lockTime.Value == false and inBattle.Value == false and inTrade.Value == false) then
sunriseWarning()
elseif (currentTimeofDay.Value == "Dusk" and lockTime.Value == false and inBattle.Value == false and inTrade.Value == false) then
sunsetWarning()
end
end
return dayNightWarningsModule
local Lighting = game:GetService("Lighting")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local dayNightCycleModule = require(ReplicatedStorage.Modules.Services["Day/Night Service"].dayNightCycleModule)
local dayNightTransitionModule = require(ReplicatedStorage.Modules.Services["Day/Night Service"].dayNightTransitionModule)
local dayNightWarningsModule = require(ReplicatedStorage.Modules.Services["Day/Night Service"].dayNightWarningsModule)
local lockTime = ReplicatedStorage.Values.BoolValues.lockTime
local currentTimeofDay = ReplicatedStorage.Values.StringValues.currentTimeofDay
local lockTimeConnection
local dayNightCycleConnection = lockTime.Changed:Connect(dayNightCycleModule.onLockedTimeChanged)
local dayNightTransitionConnection = currentTimeofDay.Changed:Connect(dayNightTransitionModule.onCurrentTimeChanged)
local dayNightWarningsConnection = currentTimeofDay.Changed:Connect(dayNightWarningsModule.sendWarning)
--New game settings
Lighting.ClockTime = 8
Lighting.OutdoorAmbient = Color3.fromRGB(140, 140, 140)
Lighting.FogColor = Color3.fromRGB(149, 195, 208)
Lighting.FogEnd = 750
currentTimeofDay.Value = "Morning"
--Connections
lockTimeConnection = lockTime.Changed:Connect(function()
if lockTime.Value == true then
dayNightCycleConnection:Disconnect()
dayNightTransitionConnection:Disconnect()
dayNightWarningsConnection:Disconnect()
lockTimeConnection:Disconnect()
else
dayNightCycleConnection = lockTime.Changed:Connect(dayNightCycleModule.onLockedTimeChanged)
dayNightTransitionConnection = currentTimeofDay.Changed:Connect(dayNightTransitionModule.onCurrentTimeChanged)
dayNightWarningsConnection = currentTimeofDay.Changed:Connect(dayNightWarningsModule.sendWarning)
lockTimeConnection:Disconnect()
end
end)
cant seem to figure out what the issue here is, a quick thing id try is inside the else statement where you set the connections again to include an if check and only do these lines if the connections were actually disconnected (maybe set them to nil after disconecting if you set them again in the same if statement, would help make this easier for a quick check, because my best guess is that the connection is already connected above the changed event, does its thing, and inside the changed event it gets set again and it fires again, same frame, though i cant tell for sure, and i havent worked the way you do before, so sorry in advance if im wrong)
the one at the top is needed to make the function work in the first place, and i think the second one is used to reconnect it after disconnecting, so if thats right, removing it is not the way to go, but changing the logic is. otherwise i dont see a point in the else statement having 3 connections set again the same as that, surely there must be a logic behind it, right?
Thanks for replying, your suggestion of using another check and utilizing nil worked out! Now the message is only sending once as intended. Now I just need the day and night cycle to stop when lockTime is true xD
lockTimeConnection = lockTime.Changed:Connect(function()
if lockTime.Value == true then
dayNightCycleConnection:Disconnect()
dayNightTransitionConnection:Disconnect()
dayNightWarningsConnection:Disconnect()
lockTimeConnection:Disconnect()
dayNightCycleConnection = nil
dayNightTransitionConnection = nil
dayNightWarningsConnection = nil
elseif (dayNightCycleConnection == nil and dayNightTransitionConnection == nil and dayNightWarningsConnection == nil) then
dayNightCycleConnection = lockTime.Changed:Connect(dayNightCycleModule.onLockedTimeChanged)
dayNightTransitionConnection = currentTimeofDay.Changed:Connect(dayNightTransitionModule.onCurrentTimeChanged)
dayNightWarningsConnection = currentTimeofDay.Changed:Connect(dayNightWarningsModule.sendWarning)
lockTimeConnection:Disconnect()
end
end)
hmm, if stopping the day night cycle means simply disconnecting the connection, then just replace the else if with an else like in your first version, and inside that branch make if statements for each connection you want reconnected for each condition
like if(dayNightWarningsConnection == nil)
set daynightwarnings again
do the same for daynighttransition if theres no additional condition to check
and for the daynightcycle keep it disconnected until a specific condition resets it again, whatever that might be
what i initially had in my mind kind of looked like this, in case you need additional conditions met
if locktime.value == true then
--your logic here
else
if daynightwarnings==nil then --set daynight warnings
if daynighttransition == nil then --set daynight transition
if ConditionToReactivateTimeCycle then --only now set the daynight cycle again, if its not met, leave it disconnected, so stopped
edit: i may have misunderstood your post, so this reply might be half useless, though you could use the logic i gave you in the code sample if needed, i think it gives more freedom for you to tweak instead of checking if theyre all nil at the same time, might also help in the future if you expand on it
With this suggestion implemented, my day and night cycle appears to be working as intended now. Thank you a ton for the help :^)
lockTimeConnection = lockTime.Changed:Connect(function()
if lockTime.Value == true then
dayNightCycleConnection:Disconnect()
dayNightTransitionConnection:Disconnect()
dayNightWarningsConnection:Disconnect()
lockTimeConnection:Disconnect()
dayNightCycleConnection = nil
dayNightTransitionConnection = nil
dayNightWarningsConnection = nil
else
if dayNightCycleConnection == nil then
dayNightCycleConnection = lockTime.Changed:Connect(dayNightCycleModule.onLockedTimeChanged)
end
if dayNightTransitionConnection == nil then
dayNightTransitionConnection = currentTimeofDay.Changed:Connect(dayNightTransitionModule.onCurrentTimeChanged)
end
if dayNightWarningsConnection == nil then
dayNightWarningsConnection = currentTimeofDay.Changed:Connect(dayNightWarningsModule.sendWarning)
end
lockTimeConnection:Disconnect()
end
end)