For loop runs twice. Why?

I never knew I’d have a problem like this, but oh well.

Hi everyone! I made a door script for a project, and I’ve managed to make a good system! However, I tried modifying the script to be client side and to affect multiple doors separately, stored in a folder.
I’ve ran into a quite weird issue. My for loop that iterates through the doors in the folder runs twice, even though there is only one door? I’ve put a print statement at the start of the loop, and it clearly shows that it’s running twice.

Code: [Location: StarterPlayerScripts]

local doors = workspace:WaitForChild("Doors")

local openAngle = 95

local debounce = {}

local openInfo = TweenInfo.new(1.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local closeInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In)

for i, door in pairs(doors:GetChildren()) do
	
	local hinge = door:WaitForChild("Hinge")
	local base = door:WaitForChild("Base")
	local prompt = door:FindFirstChildWhichIsA("ProximityPrompt", true)
	
	print("Running")
	
	local pivot = hinge.CFrame
	
	door:SetAttribute("Status", false)
	
	function Tween(obj:Instance, info:TweenInfo, property, value)
		return game:GetService("TweenService"):Create(obj, info, {[property] = value})
	end

	function updateStatus(status:boolean)
		local info = nil
		local value = nil

		if status == true then
			value = pivot * CFrame.Angles(0, math.rad(openAngle), 0)
			info = openInfo
		else
			value = pivot
			info = closeInfo
		end
		
		print("STATUS:", status)
		
		local tween = Tween(hinge, info, "CFrame", value)
		tween:Play()
		door:SetAttribute("Status", status)
		tween.Completed:Wait()
		task.wait(0.05)
		--print("thing ended")
	end

	prompt.Triggered:Connect(function()
		--print("running")

		if table.find(debounce, door) then return end

		--print("debounce check passed")
		
		local isOpen = door:GetAttribute("Status")
		
		table.insert(debounce, door)
		updateStatus(not isOpen)
		
		local index = table.find(debounce, door)
		if index then table.remove(debounce, index) end
	end)
end

Output:

image

Everything duplicating

image

Clearly only one door inside

  • Yes, I’ve referenced the doors folder properly.
  • No, there aren’t any other scripts that could be interfering with the doors, only two scripts are in StarterPlayerScripts, and the other one is for a custom proximityPrompt that has nothing to do with the door system.

I’m very sorry in advance if the issue is extremely easy to solve. I’ve kinda drifted away from scripting, so I might not have the same knowledge I did before.

By any chance, did you maybe use a script with RunContext.Client? In that case two independent instances of the same script are ran, the original, and the copy in Player.

I actually did put the script into Runcontext.Client, but I ended up changing the script to a localScript instead using the reclass plugin.

Alright, I should’ve known better.

I guess a reclassed LocalScript isn’t equal to a raw one, I ended up copying the code into a brand new LocalScript, and it works perfectly.

Thanks!

1 Like

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