Using replicaservice with my inventory system

I have done the basic stuff with it, but when the player resets it doesn’t load/data can’t be grabbed. Does anyone know the issue?

Heres the script, its messy lol

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()

local RS = game:GetService("ReplicatedStorage")
local Controller = require(game:GetService("ReplicatedStorage"):WaitForChild("GetData"):WaitForChild("ReplicaController"))

local Replica

--local INV = RS:WaitForChild("PlayerInventory"):FindFirstChild(plr.Name)
local INFO = require(RS:WaitForChild("ItemInformation"))

local function clearGui()
	for i,v in pairs(script.Parent.Holder:GetChildren()) do
		if v:IsA("Frame") then
			v:Destroy()
		end
	end
end

local function checkIfequipped(item)
	local gui = script.Parent.ItemStats.Equip
	local ITEM = plr.Backpack:FindFirstChild(item) or plr.Character:FindFirstChild(item) 
	if ITEM then
		gui.Text = "Unequip"
	else
		gui.Text = "Equip"
	end
end

local function loadtransformation()
	repeat task.wait() until Replica ~= nil
	for _,ITEM in pairs(Replica.Data.inventory) do
		local ITEM_INFO = INFO[ITEM]
		if ITEM_INFO then
			if ITEM_INFO["Type"] == "Transformation" then
				local GUI = script.Item:Clone()
				GUI.Name = ITEM

				local findItem = plr.Backpack:FindFirstChild(ITEM) or plr.Character:FindFirstChild(ITEM)
				if findItem then
					GUI.LayoutOrder = 0
					GUI.Background.UIStroke.Enabled = true
				end

				GUI.Trigger.Text = GUI.Name

				GUI.Parent = script.Parent.Holder
				GUI.Trigger.MouseButton1Click:Connect(function()
					local itemstat = script.Parent:WaitForChild('ItemStats')
					itemstat.ITEM_NAME.Text = GUI.Name

					checkIfequipped(GUI.Name)

					for _,v2 in pairs(itemstat.Holder:GetChildren()) do
						if v2:IsA("Frame") then
							v2:Destroy()
						end
					end

					for i,v in pairs(ITEM_INFO) do
						if i == "WalkSpeed" then
							local STAT_FRAME = script.Stat:Clone()
							STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
							STAT_FRAME.Stat.NUMBER.Text = v
							STAT_FRAME.Parent = itemstat.Holder
						elseif i == "dmg" or i == "hp" then
							local STAT_FRAME = script.Stat:Clone()
							STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
							STAT_FRAME.Stat.NUMBER.Text = v.."x"
							STAT_FRAME.Parent = itemstat.Holder
						end
					end
				end)
			end
		end
	end
	--[[
	for _,ITEM in pairs(INV:GetChildren()) do
		local ITEM_INFO = INFO[ITEM.Name]
		if ITEM_INFO then
			if ITEM_INFO["Type"] == "Transformation" then
				local GUI = script.Item:Clone()
				GUI.Name = ITEM.Name
				
				local ITEM = plr.Backpack:FindFirstChild(ITEM.Name) or plr.Character:FindFirstChild(ITEM.Name)
				if ITEM then
					GUI.LayoutOrder = 0
					GUI.Background.UIStroke.Enabled = true
				end
				
				GUI.Trigger.Text = GUI.Name

				GUI.Parent = script.Parent.Holder
				GUI.Trigger.MouseButton1Click:Connect(function()
					local itemstat = script.Parent:WaitForChild('ItemStats')
					itemstat.ITEM_NAME.Text = GUI.Name
					
					checkIfequipped(GUI.Name)
					
					for _,v2 in pairs(itemstat.Holder:GetChildren()) do
						if v2:IsA("Frame") then
							v2:Destroy()
						end
					end

					for i,v in pairs(ITEM_INFO) do
						if i == "WalkSpeed" then
							local STAT_FRAME = script.Stat:Clone()
							STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
							STAT_FRAME.Stat.NUMBER.Text = v
							STAT_FRAME.Parent = itemstat.Holder
						elseif i == "dmg" or i == "hp" then
							local STAT_FRAME = script.Stat:Clone()
							STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
							STAT_FRAME.Stat.NUMBER.Text = v.."x"
							STAT_FRAME.Parent = itemstat.Holder
						end
					end
				end)
			end
		end
	end
	]]--
end

local function loadmagic()
	repeat task.wait() until Replica ~= nil
	for _,ITEM in pairs(Replica.Data.inventory) do
			local ITEM_INFO = INFO[ITEM]
			if ITEM_INFO then
				if ITEM_INFO["Type"] == "Magic" then
					local GUI = script.Item:Clone()
					GUI.Name = ITEM

					local findItem = plr.Backpack:FindFirstChild(ITEM) or plr.Character:FindFirstChild(ITEM)
					if findItem then
						GUI.LayoutOrder = 0
						GUI.Background.UIStroke.Enabled = true
					end

					GUI.Trigger.Text = GUI.Name

					GUI.Parent = script.Parent.Holder
					GUI.Trigger.MouseButton1Click:Connect(function()
						local itemstat = script.Parent:WaitForChild('ItemStats')
						itemstat.ITEM_NAME.Text = GUI.Name

						checkIfequipped(GUI.Name)

						for _,v2 in pairs(itemstat.Holder:GetChildren()) do
							if v2:IsA("Frame") then
								v2:Destroy()
							end
						end

						for i,v in pairs(ITEM_INFO) do
							if i == "DMG" or i == "UseTime" then
								local STAT_FRAME = script.Stat:Clone()
								STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
								STAT_FRAME.Stat.NUMBER.Text = v
								STAT_FRAME.Parent = itemstat.Holder
							elseif i == "HEAL" then
								local STAT_FRAME = script.Stat:Clone()
								STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
								STAT_FRAME.Stat.NUMBER.Text = v.."x"
								STAT_FRAME.Parent = itemstat.Holder
							end
						end

					end)
				end
			end
		end
end

local function loadaccessory()
	repeat task.wait() until Replica ~= nil
	for _,ITEM in pairs(Replica.Data.inventory) do
			local ITEM_INFO = INFO[ITEM]
			if ITEM_INFO then
				if ITEM_INFO["Type"] == "Accessory" then
					local GUI = script.Item:Clone()
					GUI.Name = ITEM

					GUI.Trigger.Text = GUI.Name

					local findItem = plr.Backpack:FindFirstChild(ITEM) or plr.Character:FindFirstChild(ITEM)
					if findItem then
						GUI.LayoutOrder = 0
						GUI.Background.UIStroke.Enabled = true
					end


					GUI.Parent = script.Parent.Holder
					GUI.Trigger.MouseButton1Click:Connect(function()
						local itemstat = script.Parent:WaitForChild('ItemStats')
						itemstat.ITEM_NAME.Text = GUI.Name

						checkIfequipped(GUI.Name)

						for _,v2 in pairs(itemstat.Holder:GetChildren()) do
							if v2:IsA("Frame") then
								v2:Destroy()
							end
						end

						for i,v in pairs(ITEM_INFO) do
							if i == "WalkSpeed" then
								local STAT_FRAME = script.Stat:Clone()
								STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
								STAT_FRAME.Stat.NUMBER.Text = v
								STAT_FRAME.Parent = itemstat.Holder
							elseif i == "dmg" or i == "hp" then
								local STAT_FRAME = script.Stat:Clone()
								STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
								STAT_FRAME.Stat.NUMBER.Text = v.."x"
								STAT_FRAME.Parent = itemstat.Holder
							end
						end

					end)
				end
			end
		end
end

local function loadmelee()
	repeat task.wait() until Replica ~= nil
	for _,ITEM in pairs(Replica.Data.inventory) do
			local ITEM_INFO = INFO[ITEM]
			if ITEM_INFO then
				if ITEM_INFO["Type"] == "Melee" then
					local GUI = script.Item:Clone()
					GUI.Name = ITEM

					GUI.Trigger.Text = GUI.Name

					local findItem = plr.Backpack:FindFirstChild(ITEM) or plr.Character:FindFirstChild(ITEM)
					if findItem then
						GUI.LayoutOrder = 0
						GUI.Background.UIStroke.Enabled = true
					end


					GUI.Parent = script.Parent.Holder
					GUI.Trigger.MouseButton1Click:Connect(function()
						local itemstat = script.Parent:WaitForChild('ItemStats')
						itemstat.ITEM_NAME.Text = GUI.Name

						checkIfequipped(GUI.Name)

						for _,v2 in pairs(itemstat.Holder:GetChildren()) do
							if v2:IsA("Frame") then
								v2:Destroy()
							end
						end

						for i,v in pairs(ITEM_INFO) do
							if i == "DMG" or i == "SlashTime" then
								local STAT_FRAME = script.Stat:Clone()
								STAT_FRAME.Stat.STAT_NAME.Text = string.upper(i)
								STAT_FRAME.Stat.NUMBER.Text = v
								STAT_FRAME.Parent = itemstat.Holder
							end
						end

					end)
				end
			end
		end
end

Controller.ReplicaOfClassCreated("PlrReplica//"..plr.UserId,function(replica)
	Replica = replica
	print(replica)
	replica:ListenToChange({"inventory"},function(new)
		Replica.Data.inventory = new

		clearGui()
		loadmelee()
		loadmagic()
		loadtransformation()
		loadaccessory()
	end)
end)

Controller.RequestData()

loadmelee()
loadmagic()
loadtransformation()
loadaccessory()

task.spawn(function()
	local INPUT = script.Parent.Name_INPUT

	while task.wait() do
		if INPUT.Text ~= "" then
			for i,v in pairs(script.Parent.Holder:GetChildren()) do
				if v:IsA("Frame") then
					if string.find(string.lower(v.Name),INPUT.Text) then
						v.Visible = true
					else
						v.Visible = false
					end
				end
			end
		else
			for i,v in pairs(script.Parent.Holder:GetChildren()) do
				if v:IsA("Frame") then
					v.Visible = true
				end
			end
		end
	end
end)

local STATS = script.Parent.ItemStats
STATS.Equip.MouseButton1Click:Connect(function()
	if STATS.ITEM_NAME.Text ~= "NAME" and STATS.Equip.Text == "Equip" then
		STATS.Equip.Text = "Unequip"
		script.Parent.Holder:WaitForChild(STATS.ITEM_NAME.Text).LayoutOrder = 0
		script.Parent.Holder:WaitForChild(STATS.ITEM_NAME.Text).Background.UIStroke.Enabled = true
		game:GetService("ReplicatedStorage"):WaitForChild("Output"):FireServer("EquipItem",{
			ITEM = STATS.ITEM_NAME.Text
		})
	elseif STATS.ITEM_NAME.Text ~= "NAME" and STATS.Equip.Text == "Unequip" then
		STATS.Equip.Text = "Equip"
		script.Parent.Holder:WaitForChild(STATS.ITEM_NAME.Text).LayoutOrder = 1
		script.Parent.Holder:WaitForChild(STATS.ITEM_NAME.Text).Background.UIStroke.Enabled = false
		game:GetService("ReplicatedStorage"):WaitForChild("Output"):FireServer("UnequipItem",{
			ITEM = STATS.ITEM_NAME.Text
		})
	end	
end)

script.Parent.TRANSFORMATION.MouseButton1Click:Connect(function()
	clearGui()
	loadtransformation()
end)

script.Parent.MAGIC.MouseButton1Click:Connect(function()
	clearGui()
	loadmagic()
end)

script.Parent.ACCESSORY.MouseButton1Click:Connect(function()
	clearGui()
	loadaccessory()
end)

script.Parent.MELEE.MouseButton1Click:Connect(function()
	clearGui()
	loadmelee()
end)

script.Parent.ALL.MouseButton1Click:Connect(function()
	clearGui()
	loadmelee()
	loadmagic()
	loadtransformation()
	loadaccessory()
end)

script.Parent.Exit.MouseButton1Click:Connect(function()
	script.Parent.Visible = false
end)