Attempt to index nil with 'Connect'?

Hey devs!
I’ve a problem wich is very strange.
I made a “changed” function on a string Value and it prints this errormessage :

20:38:18.829 - ServerScriptService.MainHolder:196: attempt to index nil with 'Connect'
20:38:18.830 - Stack Begin
20:38:18.830 - Script 'ServerScriptService.MainHolder', Line 196
20:38:18.831 - Stack End 

Does anyone know where is the problem and how I fix it?

My script is :

local equippedBP = plr.EquippedBackpack -- It is a stringValue

equippedBP.Changed:Connect(function()
		if equippedBP.Value ~= nil then
			if game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(equippedBP.Value) then
				equipBackpack(plr, game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(equippedBP.Value):Clone())
			end
		end
	end)

Is that the whole script? The error is on line 196

I don’t think u understand RBXScriptSignals (events)

I didn’t gave you the entire script cuz it is long, but his is my entire script :

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("Test8")

local function equipTool(plr,tool)
	local char = plr.Character
	if tool ~= nil and char ~= nil then
		if char:FindFirstChild(plr.Name.." 's Tool") then char[plr.Name.." 's Tool"]:Destroy() end
		tool.Name = plr.Name.." 's Tool"
		tool.Parent = char
	end
end

local function equipBackpack(plr,bp)
	local char = plr.Character
	if bp ~= nil and char ~= nil then
		if char:FindFirstChild(plr.Name.." 's Backpack") then char[plr.Name.." 's Backpack"]:Destroy() end
		bp.Name = plr.Name.." 's Backpack"
		bp.Parent = char
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	
	local acorns = Instance.new("IntValue",leaderstats)
	acorns.Name = "Acorns"
	
	local rebirths = Instance.new("IntValue",leaderstats)
	rebirths.Name = "Rebirths"
	
	local TotalPlrLeaves = Instance.new("IntValue",leaderstats)
	TotalPlrLeaves.Name = "Total leaves"
	
	local plrInf = Instance.new("Folder",plr)
	plrInf.Name = "PlayerInfo"
	
	local leaves = Instance.new("IntValue",plrInf)
	leaves.Name = "Leaves"
	
	local HipSize = Instance.new("IntValue",plrInf)
	HipSize.Name = "HipSize"
	
	local quest = Instance.new("IntValue",plrInf)
	quest.Name = "Quest"
	quest.Value = 1000
	
	local onTheQuest = Instance.new("IntValue",plrInf)
	onTheQuest.Name = "OnTheQuest"
	
	local playTime = Instance.new("IntValue",leaderstats)
	playTime.Name = "Play time"
	
	local backpackpower = Instance.new("NumberValue",plr)
	backpackpower.Name = "PowerBP"
	
	local equippedTool = Instance.new("StringValue",plr)
	equippedTool.Name = "EquippedTool"
	
	local equippedBP = Instance.new("StringValue",plr)
	equippedBP.Name = "EquippedBackpack"
	
	local debounceData = Instance.new("Folder",game.ServerStorage.DebounceData)
	debounceData.Name = plr.Name
	
	local Tdebounce = Instance.new("BoolValue",debounceData)
	Tdebounce.Name = "ToolDebounce"
	
	local Pdebounce = Instance.new("BoolValue",game.ReplicatedStorage.DebouncePet)
	Pdebounce.Name = plr.Name
	
	local Cdebounce = Instance.new("BoolValue",game.ReplicatedStorage.DebounceCrate)
	Cdebounce.Name = plr.Name
	
	local TBdebounce = Instance.new("BoolValue",game.ReplicatedStorage.DebounceToolBox)
	TBdebounce.Name = plr.Name
	
	local sellDeb = Instance.new("BoolValue",debounceData)
	sellDeb.Name = "SellDebounce"
	
	local Inventory = Instance.new("Folder",plr)
	Inventory.Name = "Inventory"
	
	local Tools = Instance.new("Folder",Inventory)
	Tools.Name = "Tools"
	
	local Backpacks = Instance.new("Folder",Inventory)
	Backpacks.Name = "Backpacks"
	
	local Pets = Instance.new("Folder",Inventory)
	Pets.Name = "Pets"
	
	local Up = Instance.new("BoolValue",plr)
	Up.Name = "Up"
	
	local data, VData, TData, BData, PData, ETool, EBackpack
	local success, errormessage = pcall(function()
		data = DS:GetAsync("Data-"..plr.UserId)
		VData = data.VData
		TData = data.TData
		BData = data.BData
		PData = data.PData
		ETool = data.equippedTool
		EBackpack = data.equippedBP
	end)
	
	if data ~= nil then
		acorns.Value = VData.acorns
		rebirths.Value = VData.rebirths
		TotalPlrLeaves.Value = VData.totalLeaves
		leaves.Value = VData.leaves
		HipSize.Value = VData.hipSize
		
		for _, tool in pairs(TData) do
			if tool then
				local RStool = game.ReplicatedStorage.Tools.AllTools:FindFirstChild(tool)
				if RStool then
					local newTool = Instance.new("StringValue")
					newTool.Name = tool
					newTool.Parent = Tools
				end
			end
		end
		
		if ETool ~= nil then
			equippedTool.Value = ETool
		end
		
		for _, bp in pairs(BData) do
			if bp then
				local RSbp = game.ReplicatedStorage.Backpacks.AllBackpacks:FindFirstChild(bp)
				if RSbp then
					local newBP = Instance.new("StringValue")
					newBP.Name = bp
					newBP.Parent = Backpacks
				end
			end
		end
		
		if EBackpack ~= nil then
			equippedBP = EBackpack
		end
		
		for _, pet in pairs(PData) do
			if pet then
				local RSpet = game.ReplicatedStorage.Pets.AllPets:FindFirstChild(pet)
				if RSpet then
					local newPet = RSpet:Clone()
					newPet.Parent = Pets
				end
			end
		end
	else
		acorns.Value = 0
		rebirths.Value = 0
		TotalPlrLeaves.Value = 0
		leaves.Value = 0
		HipSize.Value = 0
		onTheQuest.Value = 0
		
		local BasicTool = game.ReplicatedStorage.BasicStuff["Basic Fork"]:Clone()
		BasicTool.Parent = plr.Inventory.Tools
		
		equipTool(plr,BasicTool:Clone())
		
		local BasicBP = game.ReplicatedStorage.BasicStuff["Basic Backpack"]:Clone()
		BasicBP.Parent = plr.Inventory.Backpacks
		
		equipBackpack(plr,BasicBP:Clone())
	end
	
	plr.CharacterAdded:Connect(function(char)
		if game.ReplicatedStorage:WaitForChild("Tools"):WaitForChild("AllTools"):FindFirstChild(equippedTool.Value) then
			equipTool(plr,game.ReplicatedStorage:WaitForChild("Tools"):WaitForChild("AllTools"):FindFirstChild(equippedTool.Value):Clone())
		end
		if game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(equippedBP.Value) then
			equipBackpack(plr, game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(equippedBP.Value):Clone())
		end
	end)
		
	equippedTool.Changed:Connect(function()
		if equippedTool.Value ~= nil then
			if game.ReplicatedStorage:WaitForChild("Tools"):WaitForChild("AllTools"):FindFirstChild(equippedTool.Value) then
				equipTool(plr, game.ReplicatedStorage:WaitForChild("Tools"):WaitForChild("AllTools"):FindFirstChild(equippedTool.Value):Clone())
			end
		end
	end)
		
	game.ReplicatedStorage.Remotes.EquipTool.OnServerEvent:Connect(function(plr, toolName)
		local tool = game.ReplicatedStorage:WaitForChild("Tools"):WaitForChild("AllTools"):FindFirstChild(toolName)
		if tool and plr.Inventory.Tools:FindFirstChild(toolName) then
			plr.EquippedTool.Value = toolName
		end		
	end)
	
	equippedBP.Changed:Connect(function()
		if equippedBP.Value ~= nil then
			if game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(equippedBP.Value) then
				equipBackpack(plr, game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(equippedBP.Value):Clone())
			end
		end
	end)
		
	game.ReplicatedStorage.Remotes.EquipBackpack.OnServerEvent:Connect(function(plr, bpName)
		local bp = game.ReplicatedStorage:WaitForChild("Backpacks"):WaitForChild("AllBackpacks"):FindFirstChild(bpName)
		if bp and plr.Inventory.Backpacks:FindFirstChild(bpName) then
			plr.EquippedBackpack.Value = bpName
		end		
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local ValuesSave = {
		acorns = plr.leaderstats.Acorns.Value,
		rebirths = plr.leaderstats.Rebirths.Value,
		totalLeaves = plr.leaderstats["Total leaves"].Value,
		leaves = plr.PlayerInfo.Leaves.Value,
		hipSize = plr.PlayerInfo.HipSize.Value,
	}
	
	local ToolsSave = {}
	
	for _, tool in pairs(plr.Inventory.Tools:GetChildren()) do
		if tool then
			table.insert(ToolsSave,tool.Name)
		end
	end
	
	local BackpacksSave = {}
	
	for _, bp in pairs(plr.Inventory.Backpacks:GetChildren()) do
		if bp then
			table.insert(BackpacksSave, bp.Name)
		end
	end
	
	local PetsSave = {}
	
	for _, pet in pairs(plr.Inventory.Pets:GetChildren()) do
		if pet then
			table.insert(PetsSave, pet.Name)
		end
	end
	
	local data = {VData = ValuesSave, TData = ToolsSave, BData = BackpacksSave, PData = PetsSave, equippedTool = plr.EquippedTool.Value, equippedBP = plr.EquippedBackpack.Value}
	local success, errormessage = pcall(function()
		DS:SetAsync("Data-"..plr.UserId,data)
	end)
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
		local ValuesSave = {
			acorns = plr.leaderstats.Acorns.Value,
			rebirths = plr.leaderstats.Rebirths.Value,
			totalLeaves = plr.leaderstats["Total leaves"].Value,
			leaves = plr.PlayerInfo.Leaves.Value,
			hipSize = plr.PlayerInfo.HipSize.Value,
		}
		
		local ToolsSave = {}
		
		for _, tool in pairs(plr.Inventory.Tools:GetChildren()) do
			if tool then
				table.insert(ToolsSave,tool.Name)
			end
		end
		
		local BackpacksSave = {}
		
		for _, bp in pairs(plr.Inventory.Backpacks:GetChildren()) do
			if bp then
				table.insert(BackpacksSave, bp.Name)
			end
		end
		
		local PetsSave = {}
		
		for _, pet in pairs(plr.Inventory.Pets:GetChildren()) do
			if pet then
				table.insert(PetsSave, pet.Name)
			end
		end
		
		local data = {VData = ValuesSave, TData = ToolsSave, BData = BackpacksSave, PData = PetsSave, equippedTool = plr.EquippedTool.Value, equippedBP = plr.EquippedBackpack.Value}
		local success, errormessage = pcall(function()
			DS:SetAsync("Data-"..plr.UserId,data)
		end)
	end
end)

Line 140:

		if EBackpack ~= nil then
			equippedBP = EBackpack
		end

You’re overriding your equippedBP value with whatever is in your data store. Did you mean to do equippedBP.Value = EBackpack? That’s why you’re getting this error.

This is the sort of bug that’s really easy to make! But Typed Lua will help mitigate them.

5 Likes

Where did u get that piece of code from? Where is he doing that?

Oh nice! Thx you, you solved my problem !
See you guys!

1 Like