Door script breaks and loops actions infinitely

I tried to make a door system in my game, but for some reason, when I try to get my approach to work, the action gets looped.

for _, Door in ipairs(Folder:GetChildren()) do
	PromptService.PromptTriggered:Connect(function(TriggerPrompt, TriggerPlayer)
		if TriggerPrompt.Name == "DoorPrompt" then
			for _, CharacterChild in ipairs(TriggerPlayer.Character:GetChildren()) do
				for _, Backpack in ipairs(TriggerPlayer.Backpack:GetChildren()) do
					if table.find(ConfigClearTeam, TriggerPlayer.Team.Name) or table.find(ConfigClearCard, Backpack.Name) or table.find(ConfigClearCard, CharacterChild.Name) then
						if not IsOpen then
							IsOpen = true
							TweenService:Create(DoorPart, TweenInfoVal, {CFrame = DoorPart.CFrame*CFrame.new(Vector3.new(0,DoorPart.Size.Y,0))}):Play()
							wait(6)
							TweenService:Create(DoorPart, TweenInfoVal, {CFrame = DoorPart.CFrame*CFrame.new(Vector3.new(0,-DoorPart.Size.Y,0))}):Play()
							IsOpen = false
						end
					end
				end
			end
		end
	end)
end

By doing that, I intended the doors to open for certain teams or those with different tools each with different names. Does anyone know why it gets looped infinitely?
Clip for example here:

have you tried using tween.complete?

That wouldn’t fix it, It’s looping inside a for, checking if it’s completed is not useful.

This code is embedded inside the for loop that searches the Character & the for loop that searches the Backpack, so if the if table.find(ConfigClearTeam, TriggerPlayer.Team.Name) or table.find(ConfigClearCard, Backpack.Name) or table.find(ConfigClearCard, CharacterChild.Name) then returns true for more than one of the items in the character/backpack, it’ll tween again

Add a break after the IsOpen = false line to break the loop

edited code:

Tried it, but it still does that weird loop.

Related to this, I believe it’s because I used more for loops, but without them, I wouldn’t know how to get the names of each descendant.

You could try creating & playing the tween outside of the for loops, that way the tweens can’t be repeated at all

The variable “canBeUsed” is assigned as false before the loops begin - and if the door can be opened/closed, it’s assigned true
Outside of these loops, it then checks if that variable is true, and if it is, it creates & plays the tweens

Should be working, thank you. [writing for words limit]

1 Like