Help with saving hats

  1. What do you want to achieve? Keep it simple and clear!
    I made this script that gives a hat when touch a part it works very well, but i want to know if have any method to
    Save Player Hats/Accessory
    Remove old hats
    Give the new hat (UnicornHood)
    Wait 10 seconds
    Remove UnicornHood
    And gives the player hats/accessory back

  2. What is the issue? Include screenshots / videos if possible!
    I dont found anything for help me on Developer.Roblox website

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Yes.

local hat = game.ServerStorage.Hats.UnicornHood
script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		if not hit.Parent:FindFirstChild("UnicornHood") then
			hat:Clone().Parent = hit.Parent
		end
	end
end)

Try this

local hat = game.ServerStorage.Hats.UnicornHood
local hats = {}

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		if not hit.Parent:FindFirstChild("UnicornHood") then
			local hum = hit.Parent.Humanoid
			hats = hum:GetAccessories()
			hum:RemoveAccesories()
			local clone = hat:Clone()
			clone.Parent = hit.Parent
			wait(10)
			clone:Destroy()
			for _,v in pairs(hats) do
				hum:AddAccessory(v)
			end
		end
	end
end)
1 Like

image

Oops, Do RemoveAccessories(), I forgot to add the 2nd s

1 Like

Its working, but when going to give back the hats give this error
Maybe unlock all accessories saved works

local hat = game.ServerStorage.Hats.UnicornHood
local hats = {}

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		if not hit.Parent:FindFirstChild("UnicornHood") then
			local hum = hit.Parent.Humanoid
			for _,v in pairs(hum:GetAccessories()) do
				table.insert(hats, v:Clone())
			end
			hum:RemoveAccesories()
			local clone = hat:Clone()
			clone.Parent = hit.Parent
			wait(10)
			clone:Destroy()
			for _,v in pairs(hats) do
				hum:AddAccessory(v)
			end
		end
	end
end)

I forgot that they’re going to be all nil

1 Like

I touched the part one time and it worked
but when i touch two times, gives same error…

Try putting hats = {} in the event itself

local hat = game.ServerStorage.Hats.UnicornHood


script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		if not hit.Parent:FindFirstChild("UnicornHood") then
			local hum = hit.Parent.Humanoid
			local hats = {}
			for _,v in pairs(hum:GetAccessories()) do
				table.insert(hats, v:Clone())
			end
			hum:RemoveAccesories()
			local clone = hat:Clone()
			clone.Parent = hit.Parent
			wait(10)
			clone:Destroy()
			for _,v in pairs(hats) do
				hum:AddAccessory(v)
			end
		end
	end
end)
1 Like

It worked now! thank you :heart:

Anytime! Excuse me for all the mess ups I made, I really gotta look my code carefully haha. If you have anymore issues don’t be afraid to make another post!

2 Likes