Equip/Unequip tool button doesn't work

Hey everyone,
I don’t find the problem in my script.

What I am trying to do is a custom inventory.

Everything is working excepted my equip/Unequip button.
I put in each tool a bool value called “Equipped” and when I equip a tool, i turn it to true and if Equipped.Value = true then it should unequip the tool but the else function doesn’t run…

This is my script:

			equipButton.MouseButton1Click:Connect(function()
				local equipped = tool:WaitForChild("Equipped")
				local ServerTool = game.ReplicatedStorage.Tools.AllTools:FindFirstChild(tool.Name)
				local hum = plr.Character:FindFirstChild("Humanoid")
				if ServerTool and hum then
					if not equipped.Value then
						hum:EquipTool(ServerTool)
						equipped.Value = true
						equipButton.Text = "Unequip"
						equipButton.BackgroundColor3 = Color3.fromRGB(165, 40, 9)
						equipButton.BorderColor3 = Color3.fromRGB(126, 25, 0)
					else
						print("trigger")
						hum:UnequipTools()
						equipped.Value = false
						equipButton.Text = "Equip"
						equipButton.BackgroundColor3 = Color3.fromRGB(0, 193, 0)
						equipButton.BorderColor3 = Color3.fromRGB(0, 85, 0)
					end
				end
			end)
1 Like

Does the output print any errors? And does the else not work at all? like does “trigger” print because from the information you’ve provided it looks like that statement should work.

Well to start, I don’t think you should be naming the value after a function that’s already present in the Tool. Tool.Equipped is a event there, and you can edit it to your liking instead of making a brand new value for it! Hope this helps!

Screenshot 2020-06-07 13.02.23

So what I should do is “if tool.Equipped then” and after that I do “hum:UnequipTools()”.
I’ll try that, thx you for your awnsers.

No, there isn’t anything in the output. (30 chars)

reset (30 chars) (pls need an awnser

Equipped is an event, not a value. You would require to use :EquipTool on the humanoid to equip a tool, and :UnequipTool to unequip it.

Links:

1 Like

Yes i Know it’s what I did but my problems are :

  • When a tool is equipped, it disparears of the Inventory.
  • When a tool is unequipped, it is destroyed and not given back to the player.
  • Only the equip function is working

It seems like it is destroying after the equipped event. You would require to search through your codes and check if you have a :Destroy function.

For the tool not giving it back to the player, you would also require to clone it to the backpack

Very important note: Cloning a tool on the client won’t replicate to the server, this also the same as making a humanoid equip a tool on the client. You need to use RemoteEvent.

I don’t have any destroy() function on the tool in my code except destroying the template associated with the tool.

My backpack is a folder called Inventory in the plr and there is “Tools” inside of it because I disabled the regular backpack.

Do you want to see my entire function code because I’m lost on what should I do because everything is working excepted the equip and unequip functions.

resect (30 chars) … (30 chars)

Ok guys I tried something new following the steps of @Quwanterz.

I did a remotefunction for equip and another one for unequip and this is my server script :

game.ReplicatedStorage.Remotes.EquipTool.OnServerEvent:Connect(function(plr,toolName)
	local tool = plr.Inventory.Tools:FindFirstChild(toolName)
	local hum = plr.Character:FindFirstChild("Humanoid")
	if hum then
		for _, v in pairs(plr.Inventory.Tools:GetChildren()) do
			if v then
				local Equipped = v:FindFirstChild("Equipped").Value
				if Equipped then
					Equipped = false
					plr.Character:FindFirstChild(v.Name):Destroy()
				end
			end
		end
		local newTool = tool:Clone()
		newTool.Parent = game.ServerStorage
		hum:EquipTool(newTool)
		tool:WaitForChild("Equipped").Value = true
	end
end)

game.ReplicatedStorage.Remotes.UnequipTool.OnServerEvent:Connect(function(plr,toolName)
	local tool = plr.Inventory.Tools:FindFirstChild(toolName)
	print(tool.Name)
	local hum = plr.Character:FindFirstChild("Humanoid")
	if hum then
		print(hum.Parent.Name)
		local Equipped = tool:WaitForChild("Equipped").Value
		if Equipped then
			hum:UnequipTools()
			Equipped = false
		end
	end
end)

Everythings look working but i doesn’t equip the tool, I think the error comes from the "hum:EquipTool(newTool) because I cloned the tool (wich is in the inventory).

Does anyone has an idea to fix that?

when i try equipping a tool i put it inside the character so you would have to destroy it or else you have to put it in workspace etc.

So your advice is to replcace : “hum:EquipTool” by:

(Equipfunction)

local newTool = tool:Clone()
newTool.Parent = plr.Character

(Unequipfunction)

local equippedTool = plr.Character:FindFirstChild(tool.Name)
equippedTool:Destroy()

Is that?

Try parenting newTool to workspace instead of ServerStorage

1 Like

yes

30char30char

It doesn’t change anything :confused:

What exactly is happening? It’s new code so, is it not equipping at all now too unlike before where it was just not unequipping?

1 Like

what is the part’s name

have you tried parenting it to the player’s character then you can use Humanoid:EquipTool(toolName)