Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Hat Inventory Glitches After Player Dies?

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

0

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?

0

The function is the server script

0

Im trying to get it to work the same even after you die

1 answer

The error I'm receiving

ServerScriptService.HatModule:173: attempt to index nil with 'Destroy'

Module

local dataStore = game:GetService("DataStoreService")
local HatsData = dataStore:GetDataStore("HatDataSavingTest01")
local module = {}


local currency = script:WaitForChild("Settings"):WaitForChild("Currency")
local maxhats = script:WaitForChild("Settings"):WaitForChild("MaxEquippableHats")
local cansavehats = false
local alredysavedhats = true

function module.loadhats(player)
	
	warn("A")
		
	local maxhats = Instance.new("IntValue",player)
	maxhats.Name = "maxHats"
	maxhats.Value = script:WaitForChild("Settings"):WaitForChild("MaxEquippableHats").Value
	
	local maxhatsinv = Instance.new("IntValue",player)
	maxhatsinv.Name = "maxHatsInv"
	maxhatsinv.Value = script:WaitForChild("Settings"):WaitForChild("MaxHatStorage").Value
	
	local Inventory = player:WaitForChild("Inventory")
	
	local Hats = Inventory:FindFirstChild("Hats")
	
	--[[Instance.new("Folder",Inventory)
	Hats.Name = "Hats"]]--
	
	local HatsTable = {}
	
	warn("A")
	
	local function dataload()
	HatsTable = HatsData:GetAsync(player.UserId)
	warn("B")
	end

	local sucess, err = pcall(dataload)

	if sucess then
		
	if not HatsTable or #HatsTable == 0 then
		alredysavedhats = false
		warn("?")
	end



	local num1 = 1
	local num2 = 2
	local num3 = 3
	local num4 = 4

	warn("D")
	
	warn(HatsData)
	
	print(".............")
	
	warn(HatsTable)
		
if HatsTable then
	
	warn("E")
	
	if #HatsTable >0 then

	for i = 1, #HatsTable / 4 do
			 
					warn("F")
					
					local hat = Instance.new("BoolValue",Hats)
						hat.Name = HatsTable[num1]
						hat.Value = HatsTable[num2]
						
					local hatimage = Instance.new("NumberValue",hat)
						hatimage.Name = "ImageID"
						hatimage.Value = HatsTable[num3]
						
					local hatrarity = Instance.new("StringValue", hat)
						hatrarity.Name = "Rarity"
						hatrarity.Value = HatsTable[num4]
						 	
						
					if num4 == #HatsTable then
						cansavehats = true
					end
						
						
						num1 = num1 + 4
						num2 = num1 + 1
						num3 = num1 + 2
						num4 = num1 + 3
						
						warn("G")
			
		end
	end
end
	else	
				print("Error while loading: "..err)
				player:Kick("Failed to load your data!")	
end
end

function module.savehats(player)
	if cansavehats == true or alredysavedhats == false then
		
		local HatsTableSave =  {}
		
		local Inv = player:WaitForChild("Inventory")
		
		local Hats = Inv:WaitForChild("Hats")
		
	if Hats then
		
	for i,v in pairs(Hats:GetChildren()) do
		table.insert(HatsTableSave, v.Name)
		table.insert(HatsTableSave, v.Value)
		table.insert(HatsTableSave, v:WaitForChild("ImageID").Value)
		table.insert(HatsTableSave, v:WaitForChild("Rarity").Value)
	end


		warn("New Table: ")
		warn(HatsTableSave)
		
		local function checking()
			HatsData:SetAsync(player.UserId, HatsTableSave)
		end
		
		checking()

		local sucess, err = pcall(checking)
		
		if sucess then
					
		else
			print("Error while saving: "..err)
			local plr = player
			module.savehats(plr)
		end
		end
	end
end

function module.PlrJoinedAddHat(player)
	for i,v in pairs(player:WaitForChild("Inventory").Hats:GetChildren())do
		if v.Value == true then
			local clone = game.ServerStorage.Hats:WaitForChild(v.Name):Clone()
			clone.Name = v.Name
			
			local object = Instance.new("ObjectValue",v)
			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
end

function module.deletehat(player,hat)
	hat:Destroy()
end

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

local hats = game.ServerStorage:WaitForChild("Hats")
function getRandomRarity(hatshop)
	local tempRarityTable = {}
	for _, i in ipairs(hatshop:WaitForChild("Rarities"):GetChildren()) do
		for p = 0, i.Value do
			table.insert(tempRarityTable, i.name)
		end
	end
	return tempRarityTable[math.random(#tempRarityTable)]
end

function getRandomHatByRarity(rarity)
	local tempTable = {}
	for _, i in ipairs(hats:GetChildren()) do
		if i:WaitForChild("Rarity").Value == rarity then
			table.insert(tempTable, i)
		end
	end
	return tempTable[math.random(#tempTable)]
end


return module

2 comments

0

There's no line 173 though.

0

oops I forgot to post the entire module

This discussion is preserved as it was. New answers, comments, and votes are closed.