How to make a TextButton/ImageButton that gives you a hat in game

Hello! So, im starting a new game, and im making an system that if player awards an badge, it will give them an hat that they can equip through an UI. The problem is, i dont have any idea how to make a button that gives a hat. Already searched tutorials on devforum and youtube and the results of those was, or the hat felt down from the player, or it just spawned at the workspace. Also, im starting at scripting, and im not asking for a full code, if possible, just try to make simpler to me to understand, ill be so grateful :,)

The hats are located in ReplicatedStorage inside a folder called “Accessories”:
image

And the UI:
image
(Those buttons are to equip the accessories)

Thanks for any help! <3

1 Like

Try using remote events and from there create a script in server script service to give the hat to the player.
Example:

--Inside text button or image button
local Event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
local Hat = game:GetService("ReplicatedStorage"):WaitForChild("Accessories"):FindFirstChild("") --Hat

script.Parent.MouseButton1Down:Connect(function()
   Event:FireServer(Hat)
end)

So basically when the button is clicked it fires an event with the corresponding hat.

Give Hat:

local Event = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

Event.OnServerEvent:Connect(function(player, hat)
   local character = player.Character

   local Clone = hat:Clone()
   Clone.Parent = character
end)

How it works:
This systems works so when you click the button it fires a remote event which then equips the hat to the player.

Hope this helps!

5 Likes

It doesnt worked! Do i need to put the 2nd script at the ServerScriptStorage? And those are Local or normal scripts??

1 Like

Yes the first script is inside the button and the second script is in server script service.
First is local
Second is default

1 Like

OH NVM it was a problem in the button, thank you so much!!

2 Likes

For anyone who has no idea of how to achieve/accomplish this, here you go.

--LOCAL

local replicated = game:GetService("ReplicatedStorage")
local hatRemote = replicated:WaitForChild("HatRemote")

local button = script.Parent

local function onButtonClicked()
	hatRemote:FireServer()
end

button.MouseButton1Click:Connect(onButtonClicked)
--SERVER

local insert = game:GetService("InsertService")
local loadAsset = insert.LoadAsset
local replicated = game:GetService("ReplicatedStorage")
local hatRemote = replicated.HatRemote

local hatId = 4640898
	
local function onHatRemoteFired(player)
	if not player.Parent then return end
	local character = player.Character
	if not character then return end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid then return end
	if humanoid.Health <= 0 then return end
	local success, result = pcall(loadAsset, insert, hatId)
	if success then
		if result then
			local hat = result:FindFirstChildOfClass("Accessory")
			if not hat then return end
			humanoid:AddAccessory(hat)
		end
	else
		warn(result)
	end
end

hatRemote.OnServerEvent:Connect(onHatRemoteFired)

test.rbxl (29.5 KB)

These scripts, activated upon the click of a GuiButton object, will attach the following hat to the clicking player’s character.

4 Likes