Need help about pcalls

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)
1 Like

Why take pcall if you can do without it?
To be sure, I would take FireServer() in this function.

1 Like

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
2 Likes

Now when I do that it doesn’t say unequipped after I click it again it just spawns another pickaxe in my inventory.

local player = game.Players.LocalPlayer
wait(1)
local ReplicateddStorage = game:GetService("ReplicatedStorage")
local shopButton = player.PlayerGui.ShopButton.Frame.TextButton
local shopGui = player.PlayerGui.ShopGui
shopGui.Enabled = false

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

local cashValue = player.leaderstats.Cash

stoneButton.MouseButton1Click:Connect(function()
	stoneButton.TextLabel.Text = "EQUIP"
	stoneButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)
	local burger = player.Backpack:FindFirstChild("DiamondPickaxe") or player.Character:FindFirstChild("DiamondPickaxe")
	if burger then
		stoneButton.TextLabel.Text = "UNEQUIPPED"
		stoneButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)

		burger:destroy()
		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)

ironButton.MouseButton1Click:Connect(function()
	ironButton.TextLabel.Text = "EQUIP"
	ironButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)
	local durum = player.Backpack:FindFirstChild("DiamondPickaxe") or player.Character:FindFirstChild("DiamondPickaxe")
	if durum then
		ironButton.TextLabel.Text = "UNEQUIPPED"
		ironButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)

		durum:destroy()
		return
	end

	local stoneClone = ironPickaxe:Clone()
	stoneClone.Name = "IronPickaxe"
	stoneClone.Parent = player.Backpack

	ironButton.TextLabel.Text = "EQUIPPED"
	ironButton.TextLabel.TextColor3 = Color3.new(0, 255, 0)
end)

diamondButton.MouseButton1Click:Connect(function()
	if cashValue.Value >= 10 then
		diamondButton.TextLabel.Text = "EQUIP"
		diamondButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)
		local hotdog = player.Backpack:FindFirstChild("DiamondPickaxe") or player.Character:FindFirstChild("DiamondPickaxe")
		if hotdog then
			diamondButton.TextLabel.Text = "UNEQUIPPED"
			diamondButton.TextLabel.TextColor3 = Color3.new(170, 0, 0)
			
			hotdog:destroy()
			return
		end

		local stoneClone = diamondPickaxe:Clone()
		stoneClone.Name = "DiamondPickaxe"
		stoneClone.Parent = player.Backpack

		diamondButton.TextLabel.Text = "EQUIPPED"
		diamondButton.TextLabel.TextColor3 = Color3.new(0, 255, 0)
	end
end)

How do i that? am pretty new to scripting.

1 Like

That’s because you have copied and pasted your code and left all of these functions looking for DiamonPickaxes

1 Like

Make it easier, use this tutorial

He will show you how to make your store better