I've been trying the past 2 days to fix this but I have no clue how/why this is happening. It works perfectly until the player dies!? Once the player dies the hat is able to be equipped again, but you can unequip it?
Function
function module.equipunequip(player,state,hat)
if state == "Unequip" then
hat.Value = false
hat:WaitForChild("Object").Value:Destroy()
hat:WaitForChild("Object"):Destroy()
elseif state == "Equip" then
hat.Value = true
local clone = game.ServerStorage:WaitForChild("Hats"):WaitForChild(hat.Name):Clone()
clone.Name = hat.Name
local object = Instance.new("ObjectValue",hat)
object.Name = "Object"
object.Value = clone
repeat wait() until game.Workspace:FindFirstChild(player.Name)
local char = game.Workspace:WaitForChild(player.Name)
char:WaitForChild("Humanoid"):AddAccessory(clone)
end
end
Hat Inventory Script
local plr = game.Players.LocalPlayer
local Inv = plr:WaitForChild("Inventory")
local hatInventory = Inv:WaitForChild("Hats")
local hatTemplate = script.Parent.Parent:WaitForChild("hatTemplate")
local mainframe = script.Parent
local hatCounter = script.Parent:WaitForChild("EquippedHatsCounter")
local hatStorageCounter = script.Parent:WaitForChild("HatsStoredCounter")
local inventoryFrame = script.Parent:WaitForChild("Inventory")
local equipunequipEVENT = game.ReplicatedStorage.HatEvents:WaitForChild("Equip/Unequip Hat")
local deletehatEVENT = game.ReplicatedStorage.HatEvents:WaitForChild("DeleteHat")
local maximumEquippedHats = plr:WaitForChild("maxHats").Value
local maximumHatsInInventory = plr:WaitForChild("maxHatsInv").Value
local rarityOrder = {
Mythical = 1,
Legendary = 3,
Rare = 7,
Uncommon = 9,
Common = 11,
}
local rarityColorOrder = {
Common = Color3.fromRGB(169, 163, 163),
Uncommon = Color3.fromRGB(64, 187, 64),
Rare = Color3.fromRGB(0, 174, 255),
Legendary = Color3.fromRGB(255, 0, 0),
Mythical = Color3.fromRGB(170,0,225),
}
function updateInventory()
local numberofequippedhats = 0
for _, i in ipairs(inventoryFrame:GetChildren()) do
if i:IsA("ImageLabel") then
i:Destroy()
end
end
for i,v in pairs(hatInventory:GetChildren()) do
if v.Value == true then
numberofequippedhats = numberofequippedhats + 1
hatCounter.Text = numberofequippedhats.."/"..maximumEquippedHats
end
end
hatStorageCounter.Text = #hatInventory:GetChildren().."/"..maximumHatsInInventory
for x, i in ipairs(hatInventory:GetChildren()) do
hatStorageCounter.Text = x.."/"..maximumHatsInInventory
local hatFrameClone = hatTemplate:Clone()
hatFrameClone.Visible = true
hatFrameClone:WaitForChild("Title").Text = i.Name
hatFrameClone:WaitForChild("RarityText").Text = i:WaitForChild("Rarity").Value
hatFrameClone.ImageColor3 = rarityColorOrder[i:WaitForChild("Rarity").Value]
hatFrameClone.Name = i.Name
hatFrameClone.LayoutOrder = rarityOrder[i:WaitForChild("Rarity").Value]
hatFrameClone:WaitForChild("Icon").Image = "rbxassetid://"..i:WaitForChild("ImageID").Value
hatFrameClone:WaitForChild("HatFromInventory").Value = i
hatFrameClone.Parent = inventoryFrame
if i.Value == true then
hatFrameClone.LayoutOrder = 1
hatFrameClone:WaitForChild("Button"):WaitForChild("TextButton").Text = "Unequip"
else
hatFrameClone.LayoutOrder = rarityOrder[i:WaitForChild("Rarity").Value]+1
hatFrameClone:WaitForChild("Button"):WaitForChild("TextButton").Text = "Equip"
end
inventoryFrame.CanvasSize = UDim2.new(0, inventoryFrame:WaitForChild("UIGridLayout").AbsoluteContentSize.X, 0, inventoryFrame:WaitForChild("UIGridLayout").AbsoluteContentSize.Y)
hatFrameClone:WaitForChild("Button"):WaitForChild("TextButton").MouseButton1Click:Connect(function()
if hatFrameClone:WaitForChild("Button"):WaitForChild("TextButton").Text == "Equip" then
if numberofequippedhats >= maximumEquippedHats then
numberofequippedhats = maximumEquippedHats
hatCounter.Text = numberofequippedhats.."/"..maximumEquippedHats
hatCounter.TextColor3 = Color3.new(1, 0, 0) wait(0.25) hatCounter.TextColor3 = Color3.new(1, 1, 1)
else
numberofequippedhats = numberofequippedhats + 1
hatCounter.Text = numberofequippedhats.."/"..maximumEquippedHats
equipunequipEVENT:FireServer("Equip", hatFrameClone:WaitForChild("HatFromInventory").Value)
hatFrameClone:WaitForChild("Button"):WaitForChild("TextButton").Text = "Unequip"
end
else
numberofequippedhats = numberofequippedhats - 1
hatCounter.Text = numberofequippedhats.."/"..maximumEquippedHats
equipunequipEVENT:FireServer("Unequip", hatFrameClone:WaitForChild("HatFromInventory").Value)
hatFrameClone:WaitForChild("Button"):WaitForChild("TextButton").Text = "Equip"
end
end)
hatFrameClone:WaitForChild("Delete"):WaitForChild("TextButton").MouseButton1Click:Connect(function()
if i.Value == true then
numberofequippedhats = numberofequippedhats - 1
hatCounter.Text = numberofequippedhats.."/"..maximumEquippedHats
equipunequipEVENT:FireServer("Unequip", hatFrameClone:WaitForChild("HatFromInventory").Value)
end
local returned = deletehatEVENT:InvokeServer(hatFrameClone:WaitForChild("HatFromInventory").Value)
if returned == true then
hatFrameClone:Destroy()
hatStorageCounter.Text = #hatInventory:GetChildren().."/"..maximumHatsInInventory
inventoryFrame.CanvasSize = UDim2.new(0, inventoryFrame:WaitForChild("UIGridLayout").AbsoluteContentSize.X, 0, inventoryFrame:WaitForChild("UIGridLayout").AbsoluteContentSize.Y)
end
end)
end
inventoryFrame.CanvasSize = UDim2.new(0, inventoryFrame:WaitForChild("UIGridLayout").AbsoluteContentSize.X, 0, inventoryFrame:WaitForChild("UIGridLayout").AbsoluteContentSize.Y)
end
plr:WaitForChild("Inventory").Hats.ChildAdded:Connect(updateInventory)
updateInventory()
I also made a local script to make the Button say equip after the player dies:
local plr = game.Players.LocalPlayer
local hum = plr.Character:WaitForChild("Humanoid")
if hum then
hum.Died:Connect(function()
script.Parent.Text = "Equip"
end)
end
Any fixes!?

3 comments
So you want the hat to be unable to be equipped after they equip it? And also, I noticed you are using remote events to equip the hat, so can you include your server script as well?
The function is the server script
Im trying to get it to work the same even after you die