How to equip a hat with a image button

Hello!

Any help would be appreciated!

1 Like

You can use a RemoteEvent

LocalScript:

local Button = script.Parent --button

Button.MouseButton1Click:Connect(function() --mouseclick
	game.ReplicatedStorage.RemoteEvent:FireServer("Blue Hat") --fire accessory name
end)

Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(PlayerFired, HatName) --on event
	if HatName == "Blue Hat" then --check the item name
		PlayerFired.Character.Humanoid:AddAccessory(--[[accessorylocation]]:Clone()) --clone to character
	end
end)

Sorry for my English (it is not my main language)

3 Likes

We are not allowed to write scripts for you.

But basically:

Just hook up a function that sends a signal to a remote event when the button is pressed in a local script. The server scri0t listens for that remote and clones a hat and puts it in the player.

1 Like

Adding to what has been said so far, to make it more elegant you could use a dictionary instead of doing an if else for every hat.

Changing @Yorshcyt code a bit we get this:

-- Script on server

local HatDictionary = 
{
     ["Blue hat"] = --[accessoryPath],
     ["Black fedora"] = --[accessoryPath],
     ["etc"]
}
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, hatName) -- Fires when client uses :FireServer()
		player.Character.Humanoid:AddAccessory(HatDictionary[hatName]:Clone()) --clone to character
	end
end)
4 Likes