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