Attempt To Index Nil With 'Disconnect'

So Basically I have a shop that you can purchase clothing for money. There was a problem where it was doubling the money the second time I purchased the item so I knew I had to disconnect the Purchased Event.
For some reason its giving me an error saying "attempt to index nil with disconnect’.

Variables of local script:

local YesButtonConnect, LeaveButtonConnect, NoButtonConnect
local MoneyCheck1,MoneyCheck2,MoneyCheck3
local stepped

LocalScript:

	ClickDetector1.MouseClick:Connect(function(player)
		stepped:Disconnect()
		
		--Variables
	
		local goal = {CFrame = ClotheCam1.CFrame}
		local ClotheCam1Tween = tweenservice:Create(Camera, info, goal)
		
		-- Coding
		
		ClotheCam1Tween:Play()
		BuyGui.Enabled = true
		BuyGuiTweenCreated:Play()
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Robe"
		BuyGuiFrame:WaitForChild("Cost").Text = "50"
		
		MoneyCheck1 = LeftClickToBuy.MouseButton1Click:Connect(function()
		
				local data = {Outcome = "Checking"}
				YenRemote:FireServer(data)
				
			YenRemote.OnClientEvent:Connect(function(Data)
				
				if Data.Outcome == "Invalid" then 
					BuyGuiFrame:WaitForChild("Cost").Text = "Not Enough Yen!"
					print("Not Enough Yen!")
					
				elseif Data.Outcome == "Valid" then
					BuyGuiFrame:WaitForChild("Cost").Text = "Successfully Purchased!"
					
					local data = {ClothingType = 1}
					ClothingRemote:FireServer(data)
					
					print("Completed")
				end
				
			end)

Bottom of the local script:

 LeaveButtonConnect = leave.MouseButton1Click:Connect(function()
				Gui.Enabled = false
				leaveGui.Enabled = false
				stepped:Disconnect()
				repeat Camera.CameraType = Enum.CameraType.Custom until
				Camera.CameraType == Enum.CameraType.Custom
				proximityPrompt.Enabled = true
			local ClotheCam1 = ClothesFolder:WaitForChild("Clothe1"):FindFirstChild("ClickDetector1"):Destroy()
			local ClotheCam2 = ClothesFolder:WaitForChild("Clothe2"):FindFirstChild("ClickDetector2"):Destroy()
			local ClotheCam3 = ClothesFolder:WaitForChild("Clothe3"):FindFirstChild("ClickDetector3"):Destroy()
			YesButtonConnect:Disconnect()
			LeaveButtonConnect:Disconnect()
			NoButtonConnect:Disconnect()
			MoneyCheck1:Disconnect()
			MoneyCheck2:Disconnect()
			BuyGui.Enabled = false
			end)
			end)
			
	end)

image

I can’t see where stepped is not nil. In the function you are disconnecting nil without a function being added to the variable.

I’ll add the full script this may be hard to read since im a new scripter:

local Dialogueremote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("DialougeRemote")
local ClothingRemote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("ClothingTypeRemote")
local YenRemote = game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("Remote")
local userinputservice = game:GetService("UserInputService")
local tweenservice = game:GetService("TweenService")
local Camera = game.Workspace.CurrentCamera
local CameraFolder = game.Workspace:WaitForChild("Shop"):WaitForChild("ShopCameras")
local Camera1 = CameraFolder:WaitForChild("Camera1")
local Camera2 = CameraFolder:WaitForChild("Camera2")
local BlackGui= script.Parent:WaitForChild("BlackGui")
local BlackFrame = BlackGui:WaitForChild("Frame")
local BuyGui = script.Parent:WaitForChild("BuyGui")
local BuyGuiFrame = BuyGui:WaitForChild("Display")
local ClothesFolder = game.Workspace.Shop.Clothes
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local leaveGui = script.Parent:WaitForChild("LeaveGui")
local runservice = game:GetService("RunService")
local mouse = game:GetService("Players").LocalPlayer:GetMouse()
local YesButtonConnect, LeaveButtonConnect, NoButtonConnect
local MoneyCheck1,MoneyCheck2,MoneyCheck3
local stepped
local maxTilt = 10


--CameraStuff
function CameraMoving(ShopCamera)

	--// Variables

	--// Move cam

	stepped = runservice.RenderStepped:Connect(function()
		Camera.CFrame = ShopCamera.CFrame * CFrame.Angles(
			math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
			math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
			0
		)
	end)
	


end


function TweenFrame(BlackFrame)
	
	
	
	local info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false ,0)
	local info2 = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
	
	local goal = {BackgroundTransparency = 0}
	local goal2 = {BackgroundTransparency = 1}
	
	local TweenCreated = tweenservice:Create(BlackFrame, info, goal)
	local TweenCreated2 = tweenservice:Create(BlackFrame, info, goal2)
	
	BlackGui.Enabled = true
	TweenCreated:Play()
	task.wait(1.5)
	TweenCreated2:Play()

end

function ShopClotheCamTween()
	--Variables
	local Char = game:GetService("Players").LocalPlayer.Character
	local LeftClickToBuy = script.Parent:WaitForChild("BuyGui"):WaitForChild("Display"):WaitForChild("Left click to buy")
	--TweenStuff

	
	
	--CAMERAS
	local ClotheCam1 = CameraFolder:WaitForChild("ClotheCam1")
	local ClotheCam2 = CameraFolder:WaitForChild("ClotheCam2")
	local ClotheCam3 = CameraFolder:WaitForChild("ClotheCam3")
	
	--CLOTHES
	local ClickPart1 = ClothesFolder:WaitForChild("Clothe1")
	local ClickPart2 = ClothesFolder:WaitForChild("Clothe2")
	local ClickPart3 = ClothesFolder:WaitForChild("Clothe3")
	
	local ClotheModel1ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel1").Shirt.ShirtTemplate
	local ClotheModel2ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel2").Shirt.ShirtTemplate
	local ClotheModel3ShirtTemplate = ClothesFolder:WaitForChild("ClotheModel3").Shirt.ShirtTemplate
	
	local ClotheModel1PantsTemplate = ClothesFolder:WaitForChild("ClotheModel1").Pants.PantsTemplate
	local ClotheModel2PantsTemplate = ClothesFolder:WaitForChild("ClotheModel2").Pants.PantsTemplate
	local ClotheModel3PantsTemplate = ClothesFolder:WaitForChild("ClotheModel3").Pants.PantsTemplate
	
	---CLICKDETECTORS
	local ClickDetector1 = Instance.new("ClickDetector")
	ClickDetector1.Name = "ClickDetector1"
	ClickDetector1.Parent = ClickPart1

	local ClickDetector2 = Instance.new("ClickDetector")
	ClickDetector2.Name = "ClickDetector2"
	ClickDetector2.Parent = ClickPart2

	local ClickDetector3 = Instance.new("ClickDetector")
	ClickDetector3.Name = "ClickDetector3"
	ClickDetector3.Parent = ClickPart3
	
	local info = TweenInfo.new(1,Enum.EasingStyle.Sine, Enum.EasingDirection.Out,0,false,0)
	local BuyGuiGoal = {Visible = true}

	local BuyGuiTweenCreated = tweenservice:Create(BuyGuiFrame, info, BuyGuiGoal)
	
	ClickDetector1.MouseClick:Connect(function(player)
		stepped:Disconnect()
		
		--Variables
	
		local goal = {CFrame = ClotheCam1.CFrame}
		local ClotheCam1Tween = tweenservice:Create(Camera, info, goal)
		
		-- Coding
		
		ClotheCam1Tween:Play()
		BuyGui.Enabled = true
		BuyGuiTweenCreated:Play()
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Robe"
		BuyGuiFrame:WaitForChild("Cost").Text = "50"
		
		MoneyCheck1 = LeftClickToBuy.MouseButton1Click:Connect(function()
		
				local data = {Outcome = "Checking"}
				YenRemote:FireServer(data)
				
			YenRemote.OnClientEvent:Connect(function(Data)
				
				if Data.Outcome == "Invalid" then 
					BuyGuiFrame:WaitForChild("Cost").Text = "Not Enough Yen!"
					print("Not Enough Yen!")
					
				elseif Data.Outcome == "Valid" then
					BuyGuiFrame:WaitForChild("Cost").Text = "Successfully Purchased!"
					
					local data = {ClothingType = 1}
					ClothingRemote:FireServer(data)
					
					print("Completed")
				end
				
			end)
				

		
			
		end)
		
	end)

	ClickDetector2.MouseClick:Connect(function(player)
		stepped:Disconnect()
		
	
		local goal = {CFrame = ClotheCam2.CFrame}

		local ClotheCam2Tween = tweenservice:Create(Camera, info, goal)
		ClotheCam2Tween:Play()
		BuyGui.Enabled = true
		BuyGuiTweenCreated:Play()
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Kimono"
		BuyGuiFrame:WaitForChild("Cost").Text = "75"
		
		MoneyCheck2 = LeftClickToBuy.MouseButton1Click:Connect(function()
			--Need to make some CharacterAdded function
			print("Test")
			local Data = {Outcome = "Checking2"}
			YenRemote:FireServer(Data)

			YenRemote.OnClientEvent:Connect(function(Data)
				if Data.Outcome == "Invalid" then
					BuyGuiFrame:WaitForChild("Cost").Text = "Not Enough Yen!"


				else
					BuyGuiFrame:WaitForChild("Cost").Text = "Successfully Purchased!"
					local data = {ClothingType = 2}
					ClothingRemote:FireServer(data)
				end
			end)

		


		end)
	end)

	ClickDetector3.MouseClick:Connect(function(player)
		stepped:Disconnect()
		

		local goal = {CFrame = ClotheCam3.CFrame}

		local ClotheCam3Tween = tweenservice:Create(Camera, info, goal)
		ClotheCam3Tween:Play()
		BuyGuiTweenCreated:Play()
		BuyGui.Enabled = true
		
		BuyGuiFrame:WaitForChild("Clothing Name").Text = "Firebending Red"
		BuyGuiFrame:WaitForChild("Cost").Text = "100"
		
		--[[Char.Shirt.ShirtTemplate = ClotheModel3ShirtTemplate
		Char.Pants.PantsTemplate = ClotheModel3PantsTemplate]]
	
		LeftClickToBuy.MouseButton1Click:Connect(function()
			--Need to make some CharacterAdded function
			local Data = {Clothing = "Clothing3"}
			


		end)
	end)
end

Dialogueremote.OnClientEvent:Connect(function(dialogue, personName, YesAnswer) -- Arguements that have been sent through server script.
	local proximityPrompt = game.Workspace:WaitForChild("Shop"):WaitForChild("ShopTender"):WaitForChild("UpperTorso"):WaitForChild("ProximityPrompt")
	proximityPrompt.Enabled = false
	local Gui = script.Parent
	Gui.Enabled = true
	
	repeat Camera.CameraType = Enum.CameraType.Scriptable until
	Camera.CameraType == Enum.CameraType.Scriptable
	Camera.CFrame = Camera1.CFrame
	CameraMoving(Camera1)
	
	local NpcText = Gui:WaitForChild("NpcText")
	
	NpcText.Text = dialogue
	local PersonNamed = Gui.NpcText:WaitForChild("PersonName")
	
	PersonNamed.Text = personName
	
	local Yes =Gui:WaitForChild("NpcText"):WaitForChild("Yes")
	local No = Gui:WaitForChild("NpcText"):WaitForChild("No")
	local leave = Gui:WaitForChild("LeaveGui"):WaitForChild("Leave")
	
	Yes.Visible = true
	No.Visible = true

	
	NoButtonConnect = No.MouseButton1Click:Connect(function()
		Gui.Enabled = false
		stepped:Disconnect()
		repeat Camera.CameraType = Enum.CameraType.Custom until
		Camera.CameraType == Enum.CameraType.Custom
		proximityPrompt.Enabled = true
		YesButtonConnect:Disconnect()

		NoButtonConnect:Disconnect()
		stepped:Disconnect()
		BuyGui.Enabled = false
		
	end)
	
	
	YesButtonConnect = Yes.MouseButton1Click:Connect(function()
	

		Gui.Enabled = false
		No.Visible = false
		Yes.Visible = false
		leave.Visible = true
		
		NpcText.Text = YesAnswer
		task.wait(1)
		TweenFrame(BlackFrame)
		stepped:Disconnect()
		
		repeat Camera.CameraType = Enum.CameraType.Scriptable until
		Camera.CameraType == Enum.CameraType.Scriptable
		Camera.CFrame = Camera2.CFrame
		
		stepped = runservice.RenderStepped:Connect(function()
			Camera.CFrame = Camera2.CFrame * CFrame.Angles(
				math.rad((((mouse.Y - mouse.ViewSizeY / 2) / mouse.ViewSizeY)) * -maxTilt),
				math.rad((((mouse.X - mouse.ViewSizeX / 2) / mouse.ViewSizeX)) * -maxTilt),
				0
			)
			
		end)
		
		task.wait()
		
		leaveGui.Enabled = true
		task.wait(0.7)
		ShopClotheCamTween()

		
		
			 LeaveButtonConnect = leave.MouseButton1Click:Connect(function()
				Gui.Enabled = false
				leaveGui.Enabled = false
				stepped:Disconnect()
				repeat Camera.CameraType = Enum.CameraType.Custom until
				Camera.CameraType == Enum.CameraType.Custom
				proximityPrompt.Enabled = true
			local ClotheCam1 = ClothesFolder:WaitForChild("Clothe1"):FindFirstChild("ClickDetector1"):Destroy()
			local ClotheCam2 = ClothesFolder:WaitForChild("Clothe2"):FindFirstChild("ClickDetector2"):Destroy()
			local ClotheCam3 = ClothesFolder:WaitForChild("Clothe3"):FindFirstChild("ClickDetector3"):Destroy()
			YesButtonConnect:Disconnect()
			LeaveButtonConnect:Disconnect()
			NoButtonConnect:Disconnect()
			MoneyCheck1:Disconnect()
			MoneyCheck2:Disconnect()
			BuyGui.Enabled = false
			end)
			end)
			
	end)

If I understand correctly, the stepped variable is assigned upon a remote receiving an even, right?

In that case I recommend to add a check:

if stepped ~= nil then
   stepped:Disconnect()
end

I’ll do that but I’m not sure if its the stepped event because it was working find before i added the moneyconnect things

Hmm so I’m still getting an error
image

Okay so when I removed moneycheck2 from the script it works. Why is that? I need to disconnect the second event Mouseclick event don’t I?

Try adding a check for every :Disconnect().