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.
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)
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
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.
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