Why is my function firing thrice?

I really don’t know why this is happening.

Code:

local servers = game.Workspace.severroom.ServerRoomServers.Server1.ColorLights
local serverfactor = 0
local CoreTempature = core.Value.Value 

---Factor Code

--ServerFactor
local function Servers()
	if servers.Color == Color3.fromRGB(255, 89, 89) then
		if CoreTempature >= 1500 then
			CoreTempature = CoreTempature + 250
			print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
		elseif CoreTempature < 1500 then
			CoreTempature = CoreTempature - 250 
			print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
		end
	elseif servers.Color == Color3.fromRGB(0, 255, 0) then
		if CoreTempature > 1500 then
			serverfactor = CoreTempature - 1500
			print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
			if serverfactor < 250 then
				CoreTempature = CoreTempature - serverfactor
				print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
			elseif serverfactor >= 250 then
				CoreTempature = CoreTempature - 250
				print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
			end
		elseif CoreTempature < 1500 then
			serverfactor = 1500 - CoreTempature
			print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
			if serverfactor < 250 then
				CoreTempature = CoreTempature + serverfactor
				print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
			elseif serverfactor >= 250 then
				CoreTempature = CoreTempature + 250
				print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")
			end
		elseif CoreTempature == 1500 then
			print("CoreTempature is 100% stable. (ServerFactor - CoreHandler)")
		end
	end
end

---Firing Factors

servers.Changed:Connect(Servers)

The part I want you to focus on is this part:

local function Servers()
	if servers.Color == Color3.fromRGB(255, 89, 89) then
		if CoreTempature >= 1500 then
			CoreTempature = CoreTempature + 250
			print("CoreTempature: " .. CoreTempature .. "-Affected By Servers")

Now for some reason, when the event at the bottom fires this function, it prints the following:


This indicates that the function is being fired 3 times.
(Just so you know, CoreTempature’s value is 1500 to begin with)
Does anyone know why it is doing this?

If you need any extra information on the issue please ask for it and I will happily provide it.

Also, this is the code that changes the color of “servers” (another server script)
Code:

local servers = game.Workspace.serverroom.ServerRoomServers.Server1.ColorLights

wait(5)

servers.Color = Color3.fromRGB(255, 89, 89)

This issue is coming from your changed event.

Here, you address the server colors. Is there a script that is constantly changing these colors, if so, then this event will keep firing, running your Servers function. Then, in your function you check if it’s a specific color and if it is, you add, minus etc. In other words, the color is being changed by something else, causing the function to run multiple times, when the color is changed, resulting in the multiple print statements.

Just realized you posted the color changer, does this script run multiple times?

Number 1:
No, I do not have any script that is constantly changing the colors. Also, color changer runs only once.

Number 2:
Even if I did, it would have run different lines. Not the same one thrice. Since it would be changing the colors, it wouldn’t always be 255, 89, 89, or persimmon. Which means it would run different if statements (particularly the elseif that checks the color for the color RGB value of 0, 255, 0), and thus doing different lines of code. In the output, you can see all of the prints are made by Line 22 (CoreHandler:22).

Are you changing other things of the part? The .Changed event runs for whenever something in the properties is changed in the part (transparency, anchored) etc. This could be causing the function to run, checking the same color and printing multiple times.

Yes, I realize that, and no I am not changing anything within the servers part except the color and the color changes only happen once, in that one server script.

I see what it is now. Basically, your checking the value, then you change it, resulting in the if statement below that to run (as the value would be different). The if statement below will check if the value is greater or lower than the previous, but this will result in that code running too, as you changed the value, then the if statement below ran as the value was greater or lower than the one you provided

Thanks for pointing out the issue. But how would I be able to fix this problem?

I suppose you could debounce it, basically, if the value was already changed, then you set the debounce value to true. Then before checking to see if the value is greater then or less then, check if your debounce is true. If it is, then we know that the value was already changed, meaning you don’t need to run the code

local debounce = false

if debounce == false then 
    debounce = true -- so that we can confirm it has been changed
    -- number check here, preform the actions as the value hasn't been changed yet
else 
    -- We know the value was changed already
end

Correct, my apologies. Forgot to add that in, just fixed