Why are both the if and elseif statement running?

Here is a script that checks and changes a string, and for some reason the function runs both statements. Of course both statements are true as it runs through them, however only 1 statement should be able to be run per event trigger, right?

And something even weirder is that it can start by going through the elseif statement, and then in the same event it switches to the if statement. I’m very confused. Here are the prints of the event being triggered twice. It is supposed to only print 2 values, however it prints 4.
image

game.Lighting.Changed:Connect(function()
	if latestZone:match("Day") then
		local sound = sounds:FindFirstChild(string.gsub(latestZone, "Day", "Night"))
		print(sound)
		if sound then
			local check = sounds:GetChildren()
			for i,v in pairs(check) do
				v:Stop()
			end
			sound:Play()
			latestZone = sound.Name
		end	

	elseif latestZone:match("Night") then
		local sound = sounds:FindFirstChild(string.gsub(latestZone, "Night", "Day"))
		print(sound)
		if sound then
			local check = sounds:GetChildren()
			for i,v in pairs(check) do
				v:Stop()
			end
			sound:Play()
			latestZone = sound.Name	
		end
	end
end)

not exactly sure, try reviewing the .Changed event itself and add a print() statement every time it fires

1 Like

it does indeed print twice. Maybe it is because the lighting object changes more than just 1 property when clocktime changes? I tried specifying the event further Lighting.Clocktime.Changed, however it gives the error “trying to index number with Changed”

alright so I found out that using :GetPropetyChangedSignal is a method of making sure no other properties are changed for whatever mysterious reasons. Now it works well with the replacement.

game.Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()

GetPropertyChangedSignal only fires when the specified property is changed, whereas Changed fires for when any property is changed.

This likely fixed it because when ClockTime is updated, so is TimeOfDay.

1 Like

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