Script repeats three times by itself

Hello! This is my second post today, because of a minor problem with my project.
The script says no anomalies detected when there are and it disrupts the game a small amount, script:

local type1 = script.Parent.Parent.Parent:WaitForChild("Type")
local room = script.Parent.Parent.Parent:WaitForChild("Room")
local reported = nil

script.Parent.MouseButton1Click:connect(function()
	reported = false
	script.Disabled = true
	script.Parent.Text = "Sent!"
	if type1.Value ~= "" and room.Value ~= "" then
		local room2 = workspace.Rooms:WaitForChild(room.Value)
		for i, v in pairs(room2.Furniture:GetChildren()) do
			if v.Anomaly.Value == true then
				reported = true
				if v.Types.Value == type1.Value then
					if v.Types.Value == "Disappearance" then
						game.ReplicatedStorage.Resolve:FireServer("Disappearance", room2)
						wait(4)
						game.SoundService.Static:Play()
						script.Parent.Parent.Parent.Static.Visible = true
						wait(5.365)
						script.Parent.Parent.Parent.Static.Visible = false
						elseif v.Types.Value == "Movement" then
						game.ReplicatedStorage.Resolve:FireServer("Movement", room2)
						wait(4)
						game.SoundService.Static:Play()
						script.Parent.Parent.Parent.Static.Visible = true
						wait(5.365)
						script.Parent.Parent.Parent.Static.Visible = false
					elseif v.Types.Value == "ExtraObj" then
						game.ReplicatedStorage.Resolve:FireServer("ExtraObj", room2)
						wait(4)
						game.SoundService.Static:Play()
						script.Parent.Parent.Parent.Static.Visible = true
						wait(5.365)
						script.Parent.Parent.Parent.Static.Visible = false
					elseif v.Types.Value == "LightAnom" then
						game.ReplicatedStorage.Resolve:FireServer("LightAnom", room2)
						wait(4)
						game.SoundService.Static:Play()
						script.Parent.Parent.Parent.Static.Visible = true
						wait(5.365)
						script.Parent.Parent.Parent.Static.Visible = false
					elseif v.Types.Value == "OpenCloseDoor" then
						game.ReplicatedStorage.Resolve:FireServer("OpenCloseDoor", room2)
						wait(4)
						game.SoundService.Static:Play()
						script.Parent.Parent.Parent.Static.Visible = true
						wait(5.365)
						script.Parent.Parent.Parent.Static.Visible = false
					elseif v.Types.Value == "PictureAnom" then
						game.ReplicatedStorage.Resolve:FireServer("PictureAnom", room2)
						wait(4)
						game.SoundService.Static:Play()
						script.Parent.Parent.Parent.Static.Visible = true
						wait(5.365)
						script.Parent.Parent.Parent.Static.Visible = false
					end
				end
			elseif v.Anomaly.Value == false then
				if script.Parent.Parent.Parent.Static.Transparency == 0 then
					if reported == false then
															script.Parent.Text = "Report"
				print("No Anomalies Detected.")
				script.Parent.Parent.Detect.Visible = true
				task.wait(1)
				script.Parent.Parent.Detect.Visible = false
				end
					end


			end
		end
	end
	script.Parent.Text = "Report"
	script.Disabled = false
	reported = false
end)

It repeats at least three times and I do not know how to fix it.
It shows no anomalies but in the game there are anomalies, then fixes it even though it said no anomalies.
Any help is appreciated!
Anomalies = Disturbance (It’s like a puzzle game you have to find out the disturbances and fix them before you lose.)

1 Like

How many children in room2.Furniture?

3 for one room 4 for another. (CharacterLimit)

Is that why the script repeats itself 3+ times? Or is it something else? Because I see this line:

Hmm, I’ll investigate it and remove some stuff that might be it.

I can’t really find any fix for this without individually scripting every part and that would take forever…

If you only need the script to do something 1 time before doing whatever again, try adding a debounce
example of debounce :

local debounce = false

while true do
    if debounce == false then
        debounce = true
        print(“run”)
        task.wait(10)
        debounce = false
    end
    task.wait()
end

I just realized this is a bad example but hopefully you get the point

So, are you saying I should add something other than the Script.Disabled part?

I just realized you used script.Disabled, though I feel it would be better to use a simple debounce variable instead of disabling the entire script

Quick question :

I know there is all ready a comment about this but did you confirm that this for loop is causing the issue?

I am pretty sure that is the issue, but I can’t remove it because the whole script would break.

If this code is the only time the variable “reported” is set to true then..


for i, v in pairs(room2.Furniture:GetChildren()) do
			if v.Anomaly.Value == true and reported == false then
				reported = true

why not add a extra check to see if reported == false?

Cause once reported is set to true the for loop can only run the following code
One Time due to the if statement checking that reported == false