Hat giver issue!

The end is to destroy any hair the player has. and the isEquipped is a bool to check if the hat is equipped or not. and here im trying to have them unequip the hat if they click the button again
image
image

That’s what im doing in the “isEquipped” boolean.

This is my local script to fire the event btw
image

LolEvent.OnServerEvent:Connect(function(Player:Player, AccessoryItSelf) -- Accessory is hat! ! ! ! needs to B E A string :)
	for _,v:Accessory in Player.Character:GetChildren() do -- destroy any other accessories first BUT exclude the actual hat?
		if v:IsA("Accessory") then
			if v.Name ~= AccessoryItSelf then
				v:Destroy()
			end
		end
	end
	if not Player.Character:FindFirstChild(AccessoryItSelf) then -- Character does not have hat equipped
		local Hat:Accessory = ServerStorage:FindFirstChild(AccessoryItSelf):Clone()
		Player.Character:FindFirstChildOfClass("Humanoid"):AddAccessory(Hat)
	else -- they do now???
		Player.Character:FindFirstChild(AccessoryItSelf):Destroy()
	end
end)

like this? Convert your hat to an accessory btw! You can use FireClient() to make the gui equipped effect thing visible if you wanna!

How do i make it an accessory?

Primarily the problem is it’s worked before exactly like this but this time the bool value was defined in the fire event script, and in the parameters. So I dont understand why it wont work like it has done numerous times before.

No need for booleans, just use the accessory name as the argument for example: “Hat”, it will look for any accessories named “Hat” in server storage then apply it to humanoid! If character already has “Hat” equipped, it gets destroyed. Afterwards we use fireclient() to indicate whether the accessory is equipped on gui or not! It’s all about simplicity and efficiently, plus it’s more accessible, meaning you only need ONE event for accessory equipping like this.

Hi, from the looks of it your just trying to make it so they can’t reequip the same item?

Yep. That’s exactly right.

30 letter limit 30 letter limit

Have you tried my snippet, plus I see you’re trying to clone a face too? Is that correct?

Face is just the name of the group model that my hat is called…
image

And i have not tried your snippet. Looks very complex, but ill give it a try

It really isn’t, We just use the accessory name as an argument for example
Event:FireServer("Hat") just make sure your accessory matches the argument name and is found in server storage. For convenience, attach an attribute to the imagebutton with the string being the accessory name so we can just use Event:FireServer(ImageButton:GetAttribute("Accessory"))

This is a right suggestion. It is always a good thing to make your code will be as simple as possible, using a parameter when you don’t have to is unefficient.

To equip, you can check if a player is already equip something

local oldHat = player.Character:FindFirstChild("Head").FindFirstChild("HatName")
if oldHat == nil then
       -- Equip
else
       -- Reequip, or unequip
end

I have to use a parameter or else the hat will only be able to be used by one person. Lua always annoying

?, What do you mean? Are you trying to equip it for every player? beacuse for fireserver() you dont need to define player argument as it is passed automatically in
OnServerEvent(Player), I use the same method for my game and it works fine?

Sure thing, just made you a simple little baseplate so you can see how simple it is!
HatReplica.rbxl (39.8 KB)

Hope this helps :slight_smile: :heart:

Accessories are really positioned for you automatically with the use of attachments, why are you using welds?

Okay this is getting confusing. lemme break it down.

I have an army game. And I want uniforms and hats. So i made a menu for players to equip a hat. But it got slightly confusing so i went for help on the dev forum. And this guy fixed it by making me add a “isEquippied” parameter in my script. And now the taking hat on/off function isn’t working.

I just replicated his script, I didn’t bother fixing code errors he just wanted help with preventing it from being added again to the players character.