Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is it not removing the value from the table?

I am trying to make a custom inventory system however when a tool gets deleted, the last tool in the toolbar will be deleted. For example, if I have tool 1 being a block, tool 2 a stick and tool 3 a shovel. When tool 1 is deleted, tool 3 will actually be deleted and 1 and 2 will stay, when it should be 2 and 3. I have tried adding prints throughout the code to see what the code is checking, the prints state that the if statement should be working, but its constantly saying its false [Not equal to.]

This is all ran on the client in a local script

The part off the code I am facing this issue with:

backpack.ChildRemoved:Connect(function(child)

	if child.Parent ~= char then
		for i,v in pairs(tools) do
			print(v, child.Name)
			if v == child.Name then
				print("Yes")
				table.remove(tools, i)
				print(tools)
				break
			end
		end
		updateHotbar()
	end
end)

For the tool examples [Block, Stick and shovel] the output would print:

  • Block Block
  • Stick Block
  • Shovel Block It is not printing Yes or the tools table at all, making me believe that is the problem yet I cant figure out why.

The full code:

game:GetService('StarterGui'):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)


local plr = game.Players.LocalPlayer
local char = plr.Character

local backpack = plr:WaitForChild("Backpack")


local tools = backpack:GetChildren()


local slotMax = 10


local hotbar = script.Parent


local numToWord = 
	{
		[1] = "One",
		[2] = "Two",
		[3] = "Three",
		[4] = "Four",
		[5] = "Five",
		[6] = "Six",
		[7] = "Seven",
		[8] = "Eight",
		[9] = "Nine",
		[10] = "Zero"
	}


local function updateHotbar()


	for i, child in pairs(hotbar:GetChildren()) do

		if child.Name == "Tool" then child:Destroy() end
	end

	for i, tool in pairs(tools) do


		if i > slotMax then return end


		local slotClone = script.Tool:Clone()
		slotClone.TextLabel.Text = i
		slotClone.TextButton.Text = tool.Name
		if tool:FindFirstChild("Gui") then
			tool.Gui.Value = slotClone
		end


		slotClone.Parent = hotbar


		if tool.Parent == char then

			slotClone.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
		end


		game:GetService("UserInputService").InputBegan:Connect(function(input, processed)

			if not processed then

				if input.KeyCode == Enum.KeyCode[numToWord[i]] then

					local Result = game.ReplicatedStorage.Remotes.EquiptTool:InvokeServer(tool, tool.Parent)     

					for x, slot in pairs(hotbar:GetChildren()) do


						if slot:IsA("Frame") then

							local tool = tools[tonumber(slot.TextLabel.Text)]

							if tool.Parent ~= char then

								slot.BackgroundColor3 = Color3.fromRGB(159, 159, 159)

							else
								slot.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
							end
						end
					end
				end
			end
		end)
	end
end
task.wait(0.5)
updateHotbar()


backpack.ChildAdded:Connect(function(child)

	if not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

backpack.ChildRemoved:Connect(function(child)

	if child.Parent ~= char then
		for i,v in pairs(tools) do
			print(v, child.Name)
			if v == child.Name then
				print("Yes")
				table.remove(tools, i)
				print(tools)
				break
			end
		end
		updateHotbar()
	end
end)


char.ChildAdded:Connect(function(child)

	if child:IsA("Tool") and not table.find(tools, child) then
		table.insert(tools, child)
		updateHotbar()
	end
end)

char.ChildRemoved:Connect(function(child)

	if child.Parent ~= backpack then
		table.remove(tools, tools[child])
		updateHotbar()
	end
end)

1 answer

✓ Accepted answer
if v == child.Name then
	print("Yes")
	table.remove(tools, i)
	print(tools)
	break
end

v will never equal child.Name because v is an Instance (You are getting the objects from the backpack, and not getting their name) and Instance.Name returns a string

--Try this!
if v.Name == child.Name then
	print("Yes")
	table.remove(tools, i)
	print(tools)
	break
end

Edit: I would recommend placing a check for if the tool is parented to the character, as it is when equipped

5 comments

0

what if you have multiple instances with the same name ?.. wouldn't you just want to check if the instance being removed is the same as the tool?

0

loowa_yawn, why would you have multiple instances in the backpack with the same name? It would confuse you and the player of your game.

0

Sorry for the late response, but thanks for this. It just proves how little things like that are easy to overlook lol.

1

Also, it does check if its parented to the character in line 108 off the full script.

0

hello uhm im sorry for the bothering but uh i didnt understand how to make that poison can you join my game and help me out? i didnt know where to send u this message so ill send it here if u dont mind

This discussion is preserved as it was. New answers, comments, and votes are closed.