Why is my script looping back to the very start once at the end? It is not indented

Ive written a script that when you press a button, the frame does a tween, disables menu gui, fires a RemoteEvent and then closes.

My issue is that my script is looping back to the start each time it gets to the end. I know this as i have a print statement at the very start, right after creating variables. Why is it doing this?

local CW = script.Parent:WaitForChild("ClassWeapons")
local G = script.Parent:WaitForChild("Gamepass")
local I = script.Parent:WaitForChild("Intermisson")
local LG = script.Parent:WaitForChild("LevelGui")
--local SG = script.Parent.ScreenGui
local MG = script.Parent:WaitForChild("MainMenu")
local INTRO = script.Parent:WaitForChild("Intro")
local MMB = script.Parent:WaitForChild("MMB")

local ATS = game:GetService("TweenService")

local rotateLoading = INTRO.Frame.Loading
local TR = 0


print("at")
CW.Enabled = false
G.Enabled = false
I.Enabled = false
LG.Enabled = false
MMB.Enabled = false
--SG.Enabled = false
MG.Enabled = true
INTRO.Enabled = false
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, false)

for a,b in pairs(INTRO.Frame:GetChildren()) do
	if not b:IsA("Script") then
		if b:IsA("Frame") then
			ATS:Create(b, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
				BackgroundTransparency = 0
			}):Play()
		elseif b:IsA("TextLabel") then
			ATS:Create(b, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
				TextTransparency = 0
			}):Play()
		elseif b:IsA("ImageLabel") then
			ATS:Create(b, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
				ImageTransparency = 0
			}):Play()
		end
	end
end

local Ttime = 0

local loadingTime = 5 -- SET YOUR LOADING TIME HERE

local function TweenIt(gui, tType)
	local TS = game:GetService("TweenService")
	if tType == "Rotate" then
		TS:Create(gui, TweenInfo.new(4, Enum.EasingStyle.Linear, Enum.EasingDirection.In, -1, false, 0), {
			Rotation = 360
		}):Play()
	elseif tType == "Invis" then
		if gui:IsA("Frame") then
			TS:Create(gui, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
				BackgroundTransparency = 1
			}):Play()
		elseif gui:IsA("TextLabel") then
			TS:Create(gui, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
				TextTransparency = 1
			}):Play()
		elseif gui:IsA("ImageLabel") then
			TS:Create(gui, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0), {
				ImageTransparency = 1
			}):Play()
		end

	end

end



for _,v in pairs(MG.Frame:GetChildren()) do

	if v:IsA("TextButton") then

		v.MouseButton1Down:Connect(function()
			INTRO.Enabled = true

			game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.All, true)

			MG.Enabled = false
			for a,b in pairs(INTRO.Frame:GetChildren()) do
				if b.Name == "Loading" then
					TweenIt(b, "Rotate")
				end
			end
			Ttime = 0
			for i=0, 100 do
				wait(0.1)
				Ttime = Ttime + 1
				INTRO.Frame.Percentage.Text = Ttime.."%"

				if i >= 0 and i <= 13 then
					INTRO.Frame.TextT.Text = "Adding you to server..."
				elseif i >= 13 and i <= 28 then
					INTRO.Frame.TextT.Text = "Loading the game..."
				elseif i >= 28 and i <= 33 then
					INTRO.Frame.TextT.Text = "Getting game info..."
				elseif i >= 33 and i <= 46 then
					INTRO.Frame.TextT.Text = "Checking saved data..."
				elseif i >= 46 and i <= 51 then
					INTRO.Frame.TextT.Text = "Checking for infections..."
				elseif i >= 51 and i <= 60 then
					INTRO.Frame.TextT.Text = "Getting other players..."
				elseif i >= 60 and i <= 79 then
					INTRO.Frame.TextT.Text = "Dubugging game..."
				elseif i >= 79 and i <= 86 then
					INTRO.Frame.TextT.Text = "Loading player settings..."
				elseif i >= 86 and i <= 92 then
					INTRO.Frame.TextT.Text = "Final check..."
				elseif i >= 92 and i <= 100 then
					INTRO.Frame.TextT.Text = "Loading player..."

				end

			end

			for a,b in pairs(INTRO.Frame:GetChildren()) do
				if not b:IsA("Script") then
					TweenIt(b, "Invis")
					INTRO.Frame.Percentage.Text = "0%"
				end
			end

			wait(1)
			print("A")
			INTRO.Enabled = false
			CW.Enabled = true
			MMB.Enabled = true
			G.Enabled = true
			--I.Enabled = true
			LG.Enabled = true
			--SG.Enabled = true
			print("B")
			TR = TR + 1

			if TR == 1 then
				game:GetService("ReplicatedStorage"):WaitForChild("FireRanks"):FireServer(v.Name)
			end
		end)
	end
end

print("ok")



MMB.MM.MouseButton1Click:Connect(function()
	print("MM")
	--func()
end)

Output:

at
ok
A
B
at
ok

and so on..

It’s highly likely you put this in a ScreenGui that either:

  • Has ResetOnSpawn set to true
  • Gets cloned to the player from the StarterGui to the PlayerGui when they join, then you spawn the character