RemoteEvents being repeated

  1. What do you want to achieve? Make the events only fire once while still being in a loop.

  2. What is the issue? 2 RemoteEvents (Day & Night) are being fired repeatedly by this script.

This script is causing the RemoteEvents (Day & Night) to repeat in a forever loop, which in turn is affecting other scripts that are using their OnClientEvent:Connect functions and causes bad performance issues.

lighting = game:GetService("Lighting")
cycleevents = game.ReplicatedStorage.Events.Cycle

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()

	if lighting.ClockTime >= 6 and lighting.ClockTime < 18 then

		cycleevents.Day:FireAllClients()

	elseif lighting.ClockTime >= 18 or lighting.ClockTime < 6 then

		cycleevents.Night:FireAllClients()
	end
end)

That’s because you need to have a separate variable to make sure that it is changing from day to night rather than just being day or night.

lighting = game:GetService("Lighting")
cycleevents = game.ReplicatedStorage.Events.Cycle

local isDay = true

lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()

	if lighting.ClockTime >= 6 and lighting.ClockTime < 18 then
		if not isDay then
			isDay = true
			cycleevents.Day:FireAllClients()
		end
	elseif lighting.ClockTime >= 18 or lighting.ClockTime < 6 then --- I don't know why you have "or" here, should definitely be an "and". Actually, you could remove this entire line and just make it say "else" and it would do the same thing.
		if isDay then
			isDay = false
			cycleevents.Night:FireAllClients()
		end
	end
end)
1 Like