When I click the button, the animation plays, sound plays and the tool is cloned and equipped. But when I click the button again, the tool is not removed from the character.
can anyone tell me why? or help me make it work.
So every time the player clicks a button, it clones the tool, if has a tool inside the button.
If the player clicks the other button, it will clone the tool within that button, but the problem is if the player clicks the same button again to stop the animation. The tool is not being destroyed.
You can probably just check if they have the tool before giving it to them.
I’m assuming this is a ClickDetector, so there’s no need for checking if the tool exists in their character.
local ClickDetector = script.Parent
local ToolName = 'Tool'
local Tool = game:GetService('ServerStorage')[ToolName] -- Example
local function MouseClick(Player)
local Backpack = Player:FindFirstChild('Backpack')
if Backpack then
local FindTool = Backpack:FindFirstChild(ToolName)
if FindTool then
FindTool:Destroy()
return -- Stop the code here.
end
local Character = Player.Character or Player.CharacterAdded:Wait()
Tool:Clone().Parent = Character -- This will make the player equip the tool.
end
end
ClickDetector.MouseClick:Connect(MouseClick)
local button = script.Parent
local hasClicked = false
local player = game.Players.LocalPlayer
button.MouseButton1Click:Connect(function()
if not hasClicked then
local tool = Instance.new("Tool")
tool.Parent = player.Backpack
hasClicked = true
else
hasClicked = false
player.Backpack:WaitForChild("Tool"):Destroy()
end
end)
You can unequip the equipped tool by calling the player.Character.Humanoid:UnequipTools() then removing the tool from the inventory or just finding the tool inside the player character model.
-- Method one
player.Character.Humanoid:UnequipTools()
player.Backpack:FindFirstChild(ToolName):Destroy()
-- Method two
player.Character:FindFristChild(ToolName):Destroy()