It seems like it still duplicates the tool
It just seems like the script isn’t going past the else statement, which is really confusing.
It’s not going into the else statement bc of this line:
local isEquipped = false
Also, I think what I said about the RemoveEvent just added confusion, so try this instead. Note that it’s referring to Tool.Name in the WaitForChild call.
local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, Tool, Frame)
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(Tool.Name):Clone()
if not isEquipped then
-- Equip tool
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
isEquipped = true
else
-- Unequip tool
if UndefinedTool then
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
UndefinedTool:Destroy()
UndefinedTool = nil
isEquipped = false
end
end
end)
Yeah but, i’m sending the name of the tool to the server, as a string.
And also, what is isEquipped supposed to be then? since its not a variable nor defined to anything.
Ah, okay. Probably better to name the arg something like ToolName then to avoid confusion.
Based on your original code, isEquipped seems to be defined somewhere else, which should be fine. It just can’t be defined within the function, because its value has to exist outside of the scope of the function for you to be able to toggle it properly.
Sorry about that, also it is changing the color of the tool to the color requested in the else statement, but it doesn’t destroy the tool, is that a problem?
Oh yeah, what it’s doing now is destroying the clone that was created within this function. In the else-statement, you would do a find in the player’s backpack for the ToolName, then if found you would destroy it.
Here is the updated code. I used ToolName as the arg. I also moved the cloning into the block that only executes if the tool is equipped. I can’t test this right now so there could be a mistake in the new code I added.
local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, ToolName, Frame)
if not isEquipped then
-- Equip tool
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(ToolName):Clone()
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
isEquipped = true
else
-- Unequip tool
local TheTool = Player.Backpack:FindFirstChild(ToolName)
if TheTool then
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
TheTool:Destroy()
isEquipped = false
end
end
end)
It seems to work perfectly, but it still duplicates the tool
I’ll head to sleep, will see your reply tomorrow, but thank you so much for your help and the time you have spent helping me, I really appreciate it
You’re welcome. I am enjoying the exercise.
I think I see what you’re trying to do now. So, you will want to check for the tool before cloning and giving it. Here is the updated code. Note that I removed the isEquipped flag, since it isn’t really necessary anymore (unless you need to use it for something else).
local Tool = game:GetService("ReplicatedStorage").Tool
Tool.OnServerEvent:Connect(function(Player, ToolName, Frame)
-- Look for the tool in player's backpack
local TheTool = Player.Backpack:FindFirstChild(ToolName)
if not TheTool then
-- Not found, so clone and equip the tool
local UndefinedTool = game:GetService("ReplicatedStorage").Tools:WaitForChild(ToolName):Clone()
UndefinedTool.Parent = Player.Backpack
Frame.BackgroundColor3 = Color3.fromRGB(85, 255, 0)
else
-- Unequip tool
TheTool:Destroy()
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
end
end)
Thank you very very much!
Also, when I click on a existing tool in my inventory, if I have it equipped, It wouldn’t destroy the already equipped tool for some reason when I try to unequip the tool, how do I counter this issue?
I’m not sure how you’re equipping the tools, so it depends on how you want to unequip too. But, to unequip any tool, the code is:
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
Humanoid:UnequipTools()
end
You may be able to just add that before destroying the tool in the previous code, if that works for your situation. Or you can call the above UnequipTools() every time (e.g. before the “if not TheTool then” statement). It depends on what the desired behavior is.
I have a button, when the player clicks on it, it gives the tool, but when they click it again, it shall destroy the tool, but when the desired tool is equipped and when I try to click it again to destroy it, it would just add another clone of the same tool and it won’t destroy the tool equipped. But lemme try this out.
It would just break the unequiping
You just want to detect whether the tool is in the Character or in the Backpack. Whenever you equip a tool, the tool moves into your Character, and when you unequip it, the tool goes back to your Backpack. You just have to mention if the tool exists in the character or in the backpack (in one line).
Proper code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ToolsFol = ReplicatedStorage:WaitForChild("Tools")
local ToolEvent = ReplicatedStorage:WaitForChild("Tool")
ToolEvent.OnServerEvent:Connect(function(player, Name, Frame)
if player.Backpack:FindFirstChild(Name) or player.Character:FindFirstChild(Name) then
-- ^^ If tool is in backpack or if it is equipped
player.Character:WaitForChild("Humanoid"):UnequipTools() -- Unequips any tool that is currently equipped
player.Backpack:FindFirstChild(Name):Destroy() -- Then destroys the specific tool
Frame.BackgroundColor3 = Color3.fromRGB(90, 170, 215)
else -- If the tool is not in the Backpack or not equipped
local CloneTool = ToolsFol:WaitForChild(Name):Clone() -- Clones the tool
CloneTool.Parent = player.Backpack -- Places the tool in the backpack
Frame.BackgroundColor3 = Color3.frmRGB(85, 255, 0)
end
end)