Debris:AddItem() throwing error

So I have this script that deletes an ingredient upon collecting it, and I was looking for ways so errors wouldn’t happen. I found out Debris was a good service that works better than :Destroy(), so I used it. For some reason I keep getting the same error:

Players.imabossxd123456.Backpack.SuperMagnet.LocalScript:37: attempt to index nil with 'Parent'

Here is my code it:

UpdateClient.OnClientEvent:Connect(function(Ingredient)
	if Ingredient == "Carrot" then

		for i, v in pairs(workspace:GetChildren()) do
			if v.Name == "Carrot" then
				if v.Container:FindFirstChild("plrInstance") then
					local CarrotValue = v.Container:FindFirstChild("plrInstance")

					if CarrotValue and CarrotValue.Value == plr.Name then
						local HRP = plr.Character.HumanoidRootPart
						local tween = TS:Create(CarrotValue.Parent, info, {CFrame = CFrame.new(HRP.Position)})

						tween:Play()
						tween.Completed:Wait()
						Debris:AddItem(CarrotValue.Parent.Parent, 0.2)
					end
				end
			end
		end
	end
end)

Basically what’s happening is I put a string value in each of the models, then looked to see if it found that players name in it, if it does, then I destroy it.

It would be helpful to have the whole script, especially because I can’t tell what line it actually errored on.
My best guess is carrotvalue.Parent doesn’t exist. But I can’t say for certain.

That is the entire script. And it says it errored on line 37

there IS no line 37, the script ends on line 21

Oh yeah I completely forgot about the other parts here is the entire script:

local Magnet = script.Parent

local back = Magnet.M.Back
local Front = Magnet.M.Front

local Speed = 15

local Equip = game:GetService("ReplicatedStorage").MagnetEquip

local TS = game:GetService("TweenService")
local info = TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

local UpdateClient = game:GetService("ReplicatedStorage").UpdateClient

local plr = game.Players.LocalPlayer

local Debris = game:GetService("Debris")

Magnet.Equipped:Connect(function()
	--plr.PlayerScripts.ORBITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT.Trail.Enabled = true
end)

UpdateClient.OnClientEvent:Connect(function(Ingredient)
	if Ingredient == "Carrot" then

		for i, v in pairs(workspace:GetChildren()) do
			if v.Name == "Carrot" then
				if v.Container:FindFirstChild("plrInstance") then
					local CarrotValue = v.Container:FindFirstChild("plrInstance")

					if CarrotValue and CarrotValue.Value == plr.Name then
						local HRP = plr.Character.HumanoidRootPart
						local tween = TS:Create(CarrotValue.Parent, info, {CFrame = CFrame.new(HRP.Position)})

						tween:Play()
						tween.Completed:Wait()
						Debris:AddItem(CarrotValue.Parent.Parent, 0.2)
					end
				end
			end
		end
	end
end)

task.spawn(function()
	while true do
		for i = 0,1,0.001*Speed do
			back.Color = Color3.fromHSV(i, 1, 1)
			wait()
		end
	end
end)

That was my guess as well, but when I was reading debris:AddItem() it said

Debris does not yield the current thread, does not require a new thread and will not error if the object is already destroyed. For this reason it is the recommended method for cleaning up objects with a fixed lifetime.

So I though I would choose this over :Destroy()

Try this:

local Magnet = script.Parent

local back = Magnet.M.Back
local Front = Magnet.M.Front

local Speed = 15

local Equip = game:GetService("ReplicatedStorage").MagnetEquip

local TS = game:GetService("TweenService")
local info = TweenInfo.new(0.3, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)

local UpdateClient = game:GetService("ReplicatedStorage").UpdateClient

local plr = game.Players.LocalPlayer

local Debris = game:GetService("Debris")

Magnet.Equipped:Connect(function()
	--plr.PlayerScripts.ORBITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT.Trail.Enabled = true
end)

UpdateClient.OnClientEvent:Connect(function(Ingredient)
	if Ingredient == "Carrot" then

		for i, v in pairs(workspace:GetChildren()) do
			if v.Name == "Carrot" then
				if v.Container:FindFirstChild("plrInstance") then
					local CarrotValue = v.Container:FindFirstChild("plrInstance")

					if CarrotValue and CarrotValue.Value == plr.Name then
						local HRP = plr.Character.HumanoidRootPart
						local tween = TS:Create(CarrotValue.Parent, info, {CFrame = CFrame.new(HRP.Position)})

						tween:Play()
						tween.Completed:Wait()
						Debris:AddItem(v.Parent, 0.2)
					end
				end
			end
		end
	end
end)

task.spawn(function()
	while true do
		for i = 0,1,0.001*Speed do
			back.Color = Color3.fromHSV(i, 1, 1)
			wait()
		end
	end
end)

It says this:

 Container is not a valid member of Model "Carrot"

For context Container is inside the “Carrot” model and it’s the primary part so I can actually tween the model.

Check the explorer after the script runs: Is Container still inside of “Carrot”?

1 Like

Was about to say this because the error was attempt to index nil

That doesn’t always mean the Parent doesn’t actually exist. Sometimes it’s a typo on the programmer’s part or some other human error.

Ok so I just tested it out and it’s still there, but when collecting multiple at the same time, the errors starts to occur. If I just collect 1, it doesn’t error.

I’m stumped, im not quite sure what the issue could be.

What if you just did
Debris:AddItem(v, 0.2)

Would you also like to see the server script? My goal here was to make a magnet that collects, but only renders on the client so op players can’t ruin the game for new players.

that’s exactly what I did in my script, it didn’t work.

I’m not sure if that would help. The error is on the client right?

Well it’s server to client so it could be either.

Like maybe if I did something wrong on the server I don’t know tbh. I should probably mention this is on a .Touched event so maybe since it fires multiple times it errors out?

1 Like

It seems you did v.Parent instead and trying to delete the workspace?