Script Support Needed

Hello I am trying to make a GUI where the things you bought is stored, and when you click equip it equips and unequips… However it works when I don’t have to pickaxe in my hand but when i take it out and try to unequip it gives me an error StonePickaxe is not a valid member of Backpack “Players.slllJakob.Backpack” It’s because it doesn’t find the pickaxe in the backpack cause it’s not there but how do I make it work and so it doesn’t give error.

wait(2)
local player = game.Players.LocalPlayer
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
local StoneValue = game.ReplicatedStorage.BuyableItems.StonePickaxe.StoneValue

stonePickaxe.Equipped:Connect(function()
	StoneValue.Value = 2
	print(StoneValue.Value)
end)

stonePickaxe.Unequipped:Connect(function()
	StoneValue.Value = 1
	print(StoneValue.Value)
end)

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)
		
		if StoneValue.Value == 2 then
			game.Workspace.Character.StonePickaxe:Destroy()
		end
		
		if StoneValue.Value == 1 then
			player.Backpack.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)

Where you remove the pickaxe from the backpack, have a check first to see if its there, then have an else statement that checks the character, as that is where tools go while theyre being used.

if player.Backpack:FindFirstChild('PickaxeNameHere') then
    player.Backpack.PickaxeNameHere:Destroy()
elseif player.Character:FindFirstChild('PickaxeNameHere') then
    player.Backpack.PickaxeNameHere:Destroy()
end

Just swap out the PickaxeNameHere with the your pickaxes name

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.