Hello. I am trying to do so when you click on a unequip button unequips your tool. It is working but it if I have my tool out in my hand I try to do it doesn’t work cause it stops the rest of the code inside the pcall. How do I fix this? if I were to change the placement of the two codes then it would only remove it if I had it in the hand.
local player = game.Players.LocalPlayer
wait(player)
local ReplicateddStorage = game:GetService("ReplicatedStorage")
local shopButton = player.PlayerGui.ShopButton.Frame.TextButton
local shopGui = player.PlayerGui.ShopGui
shopButton.MouseButton1Click:Connect(function()
shopGui.Enabled = true
end)
local exitButton = player.PlayerGui.ShopGui.Frame.ExitButton
exitButton.MouseButton1Click:Connect(function()
shopGui.Enabled = false
end)
local stoneButton = player.PlayerGui.ShopGui.Frame.ScrollingFrame.StoneButton
local ironButton = player.PlayerGui.ShopGui.Frame.ScrollingFrame.IronButton
local diamondButton = player.PlayerGui.ShopGui.Frame.ScrollingFrame.DiamondButton
local stonePickaxe = game.ReplicatedStorage.BuyableItems.StonePickaxe
local ironPickaxe = game.ReplicatedStorage.BuyableItems.IronPickaxe
local diamondPickaxe = game.ReplicatedStorage.BuyableItems.DiamondPickaxe
stoneButton.MouseButton1Click:Connect(function()
if player.Backpack:FindFirstChild("StonePickaxe") or player.Character:FindFirstChild("StonePickaxe") then
stoneButton.TextLabel.Text = "UNEQUIPPED"
stoneButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)
local success, errormessage = pcall(function() --Køre videre med koden selvom der er en error.
player.Backpack.StonePickaxe:Destroy()
player.Character.StonePickaxe:Destroy()
end)
return
end
local stoneClone = stonePickaxe:Clone()
stoneClone.Name = "StonePickaxe"
stoneClone.Parent = player.Backpack
stoneButton.TextLabel.Text = "EQUIPPED"
stoneButton.TextLabel.TextColor3 = Color3.new(0, 255, 0)
end)
You shouldn’t use a pcall here, try just checking for each item’s existence instead. Which is something you already do in the if statment, but you’d be better off to store your find first child calls!
stoneButton.MouseButton1Click:Connect(function()
local stonepickaxe = player.Backpack:FindFirstChild("StonePickaxe") or player.Character:FindFirstChild("StonePickaxe")
if stonepickaxe then
stoneButton.TextLabel.Text = "UNEQUIPPED"
stoneButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)
stonepickaxe:Destroy()
return
end