Weird problem with loop

this seems like an easy fix but i cant find it, Im having this weird problem, im running a for loop in a while wait() do because i need it to run forever and not just once but it seems to be causing it to run more than once when firing to the server how would i fix this I thought

heres a video of the problem, whats happening is before the while loop it only ran once but it did work it did change to close tween if it is open instead of sometimes just flipping to the other side, but i need the loop because without it stops on the last door in the folder and thats the only one you can open

robloxapp-20230207-1919065.wmv (5.8 MB)

local script

local touching = false

while wait() do
if touching == false then
for _, door in pairs(game.Workspace.Doors:GetChildren()) do
local character = game.Players.LocalPlayer.Character
local StaticDoorPart = door.Static

local tweenServ = game:GetService("TweenService")

print(door)

local door = door.MovePart

local tweenfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)

--Get a vector that goes from the door to the character
local doorLookVect = StaticDoorPart.CFrame.lookVector
--Get the vector where the door itself faces
local frontTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild("Front").CFrame})
local backTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild("Back").CFrame})
local staticTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild("Static").CFrame})

door.Open.Triggered:Connect(function()
	game.ReplicatedStorage.DoorEvent.Door:FireServer(door,StaticDoorPart,doorLookVect,tweenfo,frontTween,backTween,staticTween)
			touching = true
			wait(0.1)
			touching = false
		end)
	end
end	

end

server script

goes in door model

local transistion = false
game.ReplicatedStorage.DoorEvent.Door.OnServerEvent:Connect(function(plr:Player,door,StaticDoorPart,doorLookVect,tweenfo)
local open = door.Parent.Open
local doorToChar = plr.Character.HumanoidRootPart.Position - StaticDoorPart.Position
local tweenServ = game:GetService(“TweenService”)
local tweenfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
local frontTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild(“Front”).CFrame})
local backTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild(“Back”).CFrame})
local staticTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild(“Static”).CFrame})
local prompt = door.Open
open.Value = not open.Value
change(open,doorToChar,doorLookVect,frontTween,door,tweenfo,backTween,staticTween)
end)

function change(open,doorToChar,doorLookVect,frontTween,door,tweenfo,backTween,staticTween)
if open.Value == false then
if doorToChar:Dot(doorLookVect) > 0 then
if transistion == false then
transistion = true
frontTween:Play()
door.Open.Enabled = false
door.Parent.Click.unlocked:Play()
wait(tweenfo.Time)
door.Open.Enabled = true
door.Open.ObjectText = “Close”
door.Parent.Click.move:Play()
wait(1)
transistion = false
end
else
if transistion == false then
backTween:Play()
transistion = true
door.Open.Enabled = false
door.Parent.Click.unlocked:Play()
wait(tweenfo.Time)
door.Open.Enabled = true
door.Open.ObjectText = “Close”
door.Parent.Click.move:Play()
wait(1)
transistion = false
end
end
else
if transistion == false then
staticTween:Play()
transistion = true
door.Open.Enabled = false
door.Parent.Click.unlocked:Play()
wait(tweenfo.Time)
door.Open.Enabled = true
door.Open.ObjectText = “Open”
door.Parent.Click.stop:Play()
wait(1)
transistion = false
end
end
end

also no matter what way i use ``` backticks it doesnt format and i cant find a post on how to format code

If I understood right, you said that:

  • Before adding the loop, only the last door in the Folder is interactive
  • After adding the loop, the doors are now confused in whether to close or open?

Maybe you’re doing this in a loop:
```

door.Open.Triggered:Connect(function()
	game.ReplicatedStorage.DoorEvent.Door:FireServer(door,StaticDoorPart,doorLookVect,tweenfo,frontTween,backTween,staticTween)
	touching = true
	wait(0.1)
	touching = false
end)

```

This creates too many connections to the same event. Adding a loop that also creates event connections is never a good idea.

Try placing the touching variable in the for loop instead:
```

for _, door in pairs(game.Workspace.Doors:GetChildren()) do
	local character = game.Players.LocalPlayer.Character
	local StaticDoorPart = door.Static

	local tweenServ = game:GetService("TweenService")

	print(door)

	local door = door.MovePart

	local tweenfo = TweenInfo.new(0.2,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)

	--Get a vector that goes from the door to the character
	local doorLookVect = StaticDoorPart.CFrame.lookVector
	--Get the vector where the door itself faces
	local frontTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild("Front").CFrame})
	local backTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild("Back").CFrame})
	local staticTween = tweenServ:Create(door,tweenfo, {CFrame = door.Parent:WaitForChild("Static").CFrame})

	local touching = false

	door.Open.Triggered:Connect(function()
		if touching then
			return -- Ignore when "touching" is still true
		end
		game.ReplicatedStorage.DoorEvent.Door:FireServer(door,StaticDoorPart,doorLookVect,tweenfo,frontTween,backTween,staticTween)
		touching = true
		wait(0.1)
		touching = false
	end)
end

```

Also…

This looks very unnecessary.

That isn’t an actual variable, he’s just saying that this is the local script

1 Like

thanks for the reply but this changing nothing the loop still playing each second the print(door) doesnt stop while the door animation is playing and it still doesnt play the right animation, if its open sometimes it will just stay open or move to the other side