Issues with tweening multiple objects at once with RunService

I’m attempting to make a SurfaceGui in which will move the frame’s position up if the player is close, or move it down if they’re not. The issue that I’m dealing with is that it does move it up, but it doesn’t move back down when told. What’s the concurrent issue here?

--[ Variables
local rs = game:GetService("RunService")
local ts = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
--[ Setup
for _, monitor in pairs(game.Workspace:GetChildren()) do
	if monitor.Name == "Monitor" and monitor:IsA("Part") then
		rs.RenderStepped:Connect(function()
			local magnitude = (char:FindFirstChild("HumanoidRootPart").Position - monitor.Position).Magnitude
			local system = monitor.Display.System
			local frame = system.MainFrame
			local val = monitor.NumVal.Value
			local monitors = {}
			if magnitude <= 5 and not table.find(monitors, val) then -- will move up
				table.insert(monitors, val)
				local tween = ts:Create(frame, TweenInfo.new(.3, Enum.EasingStyle.Quart, Enum.EasingDirection.Out, 0, false, 0), {Position = UDim2.fromScale(0, 0)})
				tween:Play()
			elseif magnitude >= 5 and table.find(monitors, val) then -- will move down
				table.remove(monitors, val)
				local tween = ts:Create(frame, TweenInfo.new(.3, Enum.EasingStyle.Quart, Enum.EasingDirection.In, 0, false, 0), {Position = UDim2.fromScale(0, 1)})
				tween:Play()
			end
		end)
	end
end

A video showing the issue: https://gyazo.com/a0605ea76bf53af348b12bb2816386b0

1 Like

The table monitors should be outside the main for loop for this to work.
Put this line: local monitors = {} before
for _, monitor in pairs(game.Workspace:GetChildren()) do

Also, the table.remove function takes an index number, not an element. You will have to find the index:
local index = table.find(monitors,val) and then do it like table.remove(monitors,index).