Index nil with Name error!

So when I equip my weapon, it works normally, and when I unequip it works normally as well, but if I do it again it breaks and gives me an error.

This the event which gets the error:

    Event.OnServerEvent:Connect(function(player, state, item, animation)
    	local char = player.Character or player.CharacterAdded:Wait()
    	if state == "Equip" then 
    			local Humanoid = player.Character:FindFirstChildOfClass("Humanoid")
    			local bp = player.Backpack[item.Name]:Clone()
    			--Humanoid:EquipTool(clone)
    			--item2.Parent = player.Backpack
    			char.Animate.toolnone:WaitForChild("ToolNoneAnim").AnimationId = "http://www.roblox.com/asset/?id="..animation
    			Update.UpdateWeapon(player, item.Stats.Serial.Value)
    	elseif state == "UnEquip" then
    		Update.UpdateWeapon(player, 0)
    	end
    end

This is the script that fires it:

script.Parent.MouseButton1Click:Connect(function()
	if script.Parent.Parent.Parent.Name == "Sword" then
		local clone = script.Parent.Parent:Clone()
		script.Parent.Parent:Destroy()
		clone.Parent = player.PlayerGui.Inventory.Inventory.Inventory
		EquipEvent:FireServer("UnEquip")
	elseif script.Parent.Parent.Parent.Name == "Inventory" then
		local frame = script.Parent.Parent
		Check.Checkit(player, Inv_Open.Value, Char_Open.Value, frame)
	end
	
end)

So I tried many different things, and is there any way to make a better system for Equipping/Unequipping tools.

When do you fire the remote with “Equip” as the state?

My bad, forgot to show that script:

Check.Checkit = function(player, open1, open2, frame, about)
	if open1 == true and open2 == true then 
		if about == "Sword" then
			for i, v in pairs(frame:WaitForChild("ItemView"):GetChildren()) do
				EquipEvent:FireServer( "UnEquip", v, v.AttackScript.Animation.Id.Value)
			end
		elseif about == "Inv" then
			if #player.PlayerGui.Inventory.Character.Sword:GetChildren() == 1 then
				print("Something is currently equipped!")
			else
				frame.Parent = player.PlayerGui.Inventory.Character.Sword
				frame.Size = UDim2.new(1,0,1,0)
				frame.Position = UDim2.new(0.5,0,0.5,0)
				for i, v in pairs(frame.ItemView:GetChildren()) do
					EquipEvent:FireServer( "Equip", v, v.AttackScript.Animation.Id.Value)
				end
			end
		end
	end
end

Do the children of “ItemView” exist on the server? You can check that by clicking on play and clicking on “current client” to change studio to server mode.

Yeah, just checked. They appear on the server.

Edit: Sorry, I just checked it doesn’t move from the inventory to the sword frame. It only stays in the inv. The children of the ItemView are still there.

Try printing their name before sending them. Also, what are the children of itemview? like are they parts or what?

It’s a tool. I have tried printing there name, and the name gets nil after the second time doing it.

My bad I tried it before sending it and it prints the name.

Try just send the name and the other value you needed from it. Instead of sending the entire tool

This is has fixed the issue with the error, but how would I equip the weapon now?

local bp = player.Backpack[item]:Clone()
Humanoid:EquipTool(bp)

Like this?

just replace item with the name of the item

It doesn’t Equip, the item is the Items name.

oh yeah, you need to do player.Backpack:GetChildren()[item]

This doesn’t seem to work as well.

do player.Backpack:FindFirstChild(item)

If that does not work nothing with that name exists in the player backpack

1 Like

Thank you this works, I really appreciate the help!

1 Like