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)
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!
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.
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.
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).