How to make a ImageButton equip a Tool?

Hey!

I am trying to make a ImageButton equip a tool in the players Backpack. How can I do that? For an example, a custom Hotbar UI. Please note I only need just 1 ImageButton. Is there a way to get tools in the players Backpack, and show the ImageButton as the TextureId of the weapon? Please let me know how I can make this happen. Thanks!

Sincerely,

papahetfan

Let’s say we have a ScreenGui with a Frame and an ImageButton inside that frame. If you paste this into a LocalScript inside of that ImageButton, you shall have what you described. The ClassicSword is in ReplicatedStorage. This script basically takes a tool from ReplicatedStorage and clones it into the player’s backpack when the ImageButton is clicked, and also changes the ImageButton’s image to the TextureId of the sword, so the ImageButton has the same picture as the ClassicSword on it. Cheers. You can tweak this to your liking.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")

local function onButtonClick()
    local ClassicSword = ReplicatedStorage:WaitForChild("ClassicSword"):Clone()
    ClassicSword.Parent = backpack

    local imageButton = script.Parent
    imageButton.Image = ClassicSword.TextureId
end

script.Parent.MouseButton1Click:Connect(onButtonClick)

Forgive me, just saw that you wanted to equip the tool too! This script equips the tool to the player when the button is clicked. Paste this into another LocalScript inside the same ImageButton.

local Players = game:GetService("Players")

local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")

local function onButtonClick()
	local ClassicSword = backpack:WaitForChild("ClassicSword")
	if ClassicSword then
		player.Character:WaitForChild("Humanoid"):EquipTool(ClassicSword)
	end
end

script.Parent.MouseButton1Click:Connect(onButtonClick)
1 Like

Character humanoids have a function that you can use to equip tools from the backpack:

Character.Humanoid:EquipTool(Tool)

Now how to get a list of the tools in the backpack:

for i,v in pairs(Player.Backpack:GetChildren()) do
   print(v.Name)
end

Finally, getting the TextureId of a tool is as simple as finding where the Texture is being stored and getting the TextureId from it.

Example if the texture was stored in the MeshPart (which is most common):

local textureId = tool.MeshPart.TextureId
2 Likes

Seems like my first script actually keeps cloning the tool on button click, instead of just cloning it once. Replace the first LocalScript with this and see if it fixes that, keep the second LocalScript.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game:GetService("Players").LocalPlayer
local backpack = player:WaitForChild("Backpack")

local function onButtonClick()
	-- Check if the ClassicSword has already been cloned
	if backpack:FindFirstChild("ClassicSword") then
		print("ClassicSword has already been cloned")
		return
	end

	-- Clone the ClassicSword from ReplicatedStorage
	local classicSword = ReplicatedStorage.ClassicSword:Clone()
	classicSword.Parent = backpack

	-- Change the Image of the ImageButton to the TextureId of the sword
	local imageButton = script.Parent:WaitForChild("ImageButton")
	imageButton.Image = "rbxassetid://"..classicSword.TextureId
end

-- Connect the button click event to the function
local button = script.Parent
button.MouseButton1Click:Connect(onButtonClick)

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