The Parent Property is locked and doesn't change the limit counter

Hello so I have the problem that my script has the error parent property locked and also it doesn’t change the limit counter if I have the BackPack accesoire :frowning:

Script is in the StarterCharacterScripts Service!

error:

The Parent property of Players.SpiderLeander1.Backpack.Grenade is locked

code:

local limit = 5
local limit2 = 10

function updateTools()


	local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
	local char = script.Parent


	local tools = {}

	for i, child in pairs(char:GetChildren()) do
		if child:IsA("Tool") then table.insert(tools, child) end
	end

	for i, tool in pairs(backpack:GetChildren()) do
		table.insert(tools, tool)
	end	


	for i, tool in pairs(tools) do
		
		if i > limit2 and char:FindFirstChild("BackPack") then 
			tool:Destroy()
		else
			if i > limit then
				tool:Destroy()
			end
		end
	end
end



local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")

backpack.DescendantAdded:Connect(updateTools)
backpack.DescendantRemoving:Connect(updateTools)

workspace.DescendantAdded:Connect(updateTools)
workspace.DescendantRemoving:Connect(updateTools)
1 Like

This happens because you are destroying the tool and it no longer exists. You need to set the parent to nil (nothing). Try this :point_down::point_down::point_down:

local limit = 5
local limit2 = 10

function updateTools()


	local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
	local char = script.Parent


	local tools = {}

	for i, child in pairs(char:GetChildren()) do
		if child:IsA("Tool") then table.insert(tools, child) end
	end

	for i, tool in pairs(backpack:GetChildren()) do
		table.insert(tools, tool)
	end	


	for i, tool in pairs(tools) do
		
		if i > limit2 and char:FindFirstChild("BackPack") then 
			tool.Parent = nil
		else
			if i > limit then
				tool.Parent = nil
			end
		end
	end
end



local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")

backpack.DescendantAdded:Connect(updateTools)
backpack.DescendantRemoving:Connect(updateTools)

workspace.DescendantAdded:Connect(updateTools)
workspace.DescendantRemoving:Connect(updateTools)
1 Like
Something unexpectedly tried to set the parent of CrowBar to NULL while trying to set the parent of CrowBar. Current parent is Backpack
1 Like

Try using Debris, I did some quick testing and it worked for me!

(Your code modified to use Debris service)

local limit = 5
local limit2 = 10
local Debris = game:GetService("Debris")

function updateTools()


	local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")
	local char = script.Parent


	local tools = {}

	for i, child in pairs(char:GetChildren()) do
		if child:IsA("Tool") then table.insert(tools, child) end
	end

	for i, tool in pairs(backpack:GetChildren()) do
		table.insert(tools, tool)
	end	


	for i, tool in pairs(tools) do

		if i > limit2 and char:FindFirstChild("BackPack") then 
			Debris:AddItem(tool, 0)
		else
			if i > limit then
				Debris:AddItem(tool, 0)
			end
		end
	end
end



local backpack = game.Players.LocalPlayer:WaitForChild("Backpack")

backpack.DescendantAdded:Connect(updateTools)
backpack.DescendantRemoving:Connect(updateTools)

workspace.DescendantAdded:Connect(updateTools)
workspace.DescendantRemoving:Connect(updateTools)
1 Like

Didn’t worked for me now if I drop something and want to get something new it doesn’t work

1 Like