Can't get gui button to equip and unequip tool

Ok, this has been a little bit frustrating, so sorry if I sound rude at all. And sorry if I talk too much lol

I’ve been trying to get a gui button(more specifically an image button) to equip and then unequip a tool once you click it.

I tried to do it myself by parenting the tool from replicated storage to the backpack on click, it had no errors, but just didn’t do anything.
So, I checked some stuff online, even stuff from this devforum, and I tested about 3 other questions about this same type of script, none of them worked.

FYI the tool’s name is “Attack” Because it is a tool with no handle and is only used for shooting things. I thought that might help in case the no handle might mess something up, I’m really new to scripting so I dunno.

I also attempted to change the image button’s image, but, similar to what usually happens, the image wont change and it doesn’t even give me errors.

I’ve already posted a couple hours ago and I really feel like I’m just doing all this without effort, but I’ve watched many scripting tutorials and I thought I got the hang of it, but apparently I haven’t. Also, GUI is my weakness when it comes to scripting and since scripting is also my weakness when it comes to game design, I really need help to get this simple thing working, so any help is greatly appreciated.

Thanks :slight_smile:

Use Humanoid:EquipTool()

It’s is highly recomended to use it on client because it will already be replicated to the server

Then you can add a script in your GUI button like this:

local button = ----your button
local char = game.Players.LocalPlayer.Character 
local debounce = true
    button.Activated:connect(function()
          if debounce then
            char.Humanoid:EquipTool(--your tool)
          debounce = false
          else 
           char.Humanoid:UnequipTools(--your tool)
         debounce = true
        end
    end)

Thats one thing I used, but it didn’t work. Also, is there a way to unequip or is it just destroying the tool in the backpack.

Yea you can unequip the tool with humanoid:UnequipTools()

I ran the script, it says “Unable to cast value to object” on line 6

Also, is there a way to disable the backpack so you can’t see the item nor equip it by pressing 1?

if this error happens then make sure that the tool is in your backpack when calling EquipTool

using this script to disable backpack GUI

local StarterGui = game:GetService("StarterGui")
 StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

I have the tool in starterpack but it still wont equip it or unequip, and the coregui script wont work, its in a local script in workspace

this is the local script in the image button

local button = script.Parent----your button
	local char = game.Players.LocalPlayer.Character 
local debounce = true
button.Activated:connect(function()
	if debounce then
		char.Humanoid:EquipTool("Attack")
			debounce = false
	else 
		char.Humanoid:UnequipTools("Attack")
			debounce = true
	end
end)

Local scripts in the workspace won’t work, they need to be under either StarterCharacterScripts, StarterPlayerScript, StarterGui or in a model with networkOwnership over its parts. Furthermore, UnequipTools() doesn’t need any arguments, it will unequip all tools even if you put nothing in the function.

Edit: I just noticed you used a string in EquipTool(), that function requires an instance (the tool itself) and not a string with the name of the tool. Something like

local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("Attack")
if tool then
    char.Humanoid:EquipTool(tool)
end

should help

is it because of filtering enabled?

But, I still can’t seem to get the button itself to equip and unequip the tool.
And where do I put the backpack disabled script then?

The backpack disabled script should be under StarterGui or StarterCharacterScripts.

Also, do you still get that Error about casting Values to objects?

1 Like

Yeah I do still get the error, its a local script in the button so I don’t know why it cant execute the equip function.

And the disable backpack worked, thanks!

local button = script.Parent ----your button
local char = game.Players.LocalPlayer.Character 
local debounce = true button.Activated:connect(function()
 	if debounce then
                local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("Attack")
                if tool then
 		      char.Humanoid:EquipTool(tool) 			 
                      debounce = false
                end
 	else 		 
                char.Humanoid:UnequipTools()
 	        debounce = true  
        end 
end)

This should hopefully work, if not, please say what went wrong as I will try to help some more.

(Sorry if the code looks weird, I’m on my phone)

1 Like

So, it works! And then I added changing the image, but it says "image is not a valid member of ImageButton “Players.Axyndey.PlayerGui.AttackEquips.Attack”
sooo yeah. I think the error means image isn’t a property? Because it is lol.
Here’s the code I put in if you need that

local button = script.Parent ----your button
local char = game.Players.LocalPlayer.Character 
button.image = "rbxassetid://6901941676"
local debounce = true button.Activated:connect(function()
	if debounce then
		local tool = game.Players.LocalPlayer.Backpack:FindFirstChild("Attack")
		if tool then
			char.Humanoid:EquipTool(tool) 			 
			debounce = false
			button.image = "rbxassetid://6901941762"
		end
	else
		char.Humanoid:UnequipTools()
		debounce = true  
		button.image = "rbxassetid://6901941676"
	end 
end)

Its probably some stupid tiny mistake I made but idk.

you simply forgot to put a upper case ‘i’ in Image, don’t forget: Lua is case sensitive language, a single lower case letter can cause an error if in the wrong place.

button.image = "rbxassetid://6901941676" -- 'image' with a lower case 'i' isn't considered a property
2 Likes

is there a reason why something like this:

local textL = script.Parent
textL.Text = "Test"

Wont display any text(the text label is set to nothing)