Adding Hat To Inventory

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hi, my friend recently made a item system for me that basicly when you buy a hat it will be added to your inventory. I am trying to make like a case opening system with it but I am confused on how to add hat to Inventory. NOTE: I am not a scripter friend is sleeping rn
  2. What is the issue? Include screenshots / videos if possible!
    I need help adding Hat to inventory
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried asking he was busy, I tried making my own thing but I dont really understand
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local HatData = game:GetService("DataStoreService"):GetDataStore("HatTestaa")

local function equipHat(plr,Hat)
	if(Hat~=nil and plr.Character ~= nil) then
		if(plr.Character:FindFirstChild(plr.Name.." Hat")) then plr.Character[plr.Name.." Hat"]:Destroy() end
		local HatClone = Hat:Clone()
		HatClone.Name = plr.Name.." Hat"
		HatClone.Parent = plr.Character
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	local inv = Instance.new("Folder")
	inv.Name = "HatInv"
	
	local h = Instance.new("StringValue")
	h.Name = "CurrentHat"
	h.Value = ""
	
	h.Parent = plr
	inv.Parent = plr
	
	h.Changed:Connect(function()
		if(h.Value ~= nil and h.Value ~= "") then
			if(game.ReplicatedStorage:WaitForChild("Hats"):FindFirstChild(h.Value)) then
				equipHat(plr,game.ReplicatedStorage:WaitForChild("Hats"):FindFirstChild(h.Value))
			end
		end
	end)
	
	local HatKey = "Hats-"..plr.UserId
	local HatSave = HatData:GetAsync(HatKey)
	if(HatSave) then
		for i,v in pairs(HatSave) do
			local Hat = game.ReplicatedStorage.Hats:FindFirstChild(v)
			if Hat then
				game.ReplicatedStorage:WaitForChild("AddHat"):FireClient(plr,Hat)
				local HatVal = Instance.new("StringValue")
				HatVal.Name = Hat.Name
				HatVal.Parent = inv
			end
		end
	end
	
	plr.CharacterAdded:Connect(function(char)
		repeat wait() until workspace:FindFirstChild(plr.Name)
		char.Humanoid.Died:Connect(function()
			repeat wait() until workspace:FindFirstChild(plr.Name)
			equipHat(plr,game.ReplicatedStorage:WaitForChild("Hats"):FindFirstChild(h.Value))
		end)
		if(game.ReplicatedStorage:WaitForChild("Hats"):FindFirstChild(h.Value)) then
			equipHat(plr,game.ReplicatedStorage:WaitForChild("Hats"):FindFirstChild(h.Value))
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	if plr ~= nil then
		local HatKey = "Hats-"..plr.UserId
		local HatInvSave = {}
		
		for i,v in pairs(plr.HatInv:GetChildren()) do
			if v then
				if v ~= nil then
					table.insert(HatInvSave,v.Name)
				end
			end
		end
		HatData:SetAsync(HatKey,HatInvSave)
	end
end)

game.ReplicatedStorage.EquipHat.OnServerEvent:Connect(function(plr,HatName)
	local Hat = game.ReplicatedStorage.Hats:FindFirstChild(HatName)
	
	if Hat ~= nil and plr.HatInv:FindFirstChild(HatName) then
		plr.CurrentHat.Value = HatName
	end
end)

game.ReplicatedStorage.DeleteHat.OnServerEvent:Connect(function(plr,HatName)
	plr.HatInv:FindFirstChild(HatName):Destroy()
end)

game.ReplicatedStorage.BuyHat.OnServerEvent:Connect(function(plr,Hat)
	local price = game.ReplicatedStorage:WaitForChild("HatPrices")[Hat.Name].Value
	
	if plr.leaderstats.Clicks.Value >= price and price >= 0 then
		plr.leaderstats.Clicks.Value = plr.leaderstats.Clicks.Value - price
		local HatO = Instance.new("StringValue",plr.HatInv)
		HatO.Name = Hat.Name
		game.ReplicatedStorage.AddHat:FireClient(plr,Hat)
	end 
end)

game.ReplicatedStorage.UnequipHat.OnServerEvent:Connect(function(plr,HatName)
	plr.CurrentHat.Value = ""
	if(plr.Character:FindFirstChild(plr.Name.." Hat")) then
		plr.Character[plr.Name.." Hat"]:Destroy()
	end
end)

local clickDetector = script.Parent
RS = game:GetService("ReplicatedStorage")
local DataModule = require(RS:WaitForChild("DataModule"))
local Pets = RS:WaitForChild("Pets")

local function ChoosePet(player)
    local P = player:WaitForChild("PlayerGui")
    local MainGui = P.MainGui
    local EggIcon = MainGui.EggIcon
    local PetImages = MainGui.PetImages

    local timer = tick()
    EggIcon.Visible = true
    while tick() - timer < DataModule.EggOpeningLength do 
        wait(.1)
        EggIcon.Rotation = math.random(-9,9)
    end

    local PetNumber = math.random(1,100)
    local petFound = false
    local pet = nil
    while petFound == false do
        for i, v in pairs(DataModule.Pets) do
            if math.random(1,v) == petNumber then
                petFound = true
                pet = i
            end
        end
    end

    EggIcon.Visible = false




    return pet
end

clickDetector.MouseClick:Connect(function(player)

    if player.leaderstats.Clicks.Value >= DataModule.EggCost then
        player.leaderstats.Clicks.Value = player.leaderstats.Clicks.Value - DataModule.EggCost

        local petOpened = ChoosePet(player)
        print(petOpened)

        local pet = Pets:FindFirstChild(petOpened):Clone()

        --Gives Hat to Inventory (I dont know how to add)
    end
end)

This is HatOpener or Handler
This is the Hat Inventory Handler
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

You could do:

local hat = Instance.new(“Tool”)
hat.Name = – hat name
hat.Parent = – inventory
local handle = Instance.new(“Accessory”)
handle.Parent = hat
handle.Name = “Handle”

Your friend should be able to get the rest.

3 Likes