Inventory system issue

Having a very weird issue with my inventory system. Everything works fine until i add a custom startercharacter. The inventory wont open and the tools in my tool bar also dissapear when using a custom character.

I’ve posted this before with no help, it seems nobody can figure this thing out. But i figured id give it one last try.

local inventoryGui = script.Parent.Parent
local slots = inventoryGui.Inventory.SlotFrame.Half.Slots:GetChildren()
local toolbar = inventoryGui.Toolbar.Holder
local player = game.Players.LocalPlayer
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")
local Run = game:GetService("RunService")

local GetThumbnails = RS:WaitForChild("GetThumbnails")
local Inventory = RS:WaitForChild("Inventory")
local Toolbar = RS:WaitForChild("Toolbar")
local BreakResource = RS:WaitForChild("BreakResource")
local thumbnailsReceived = false
local thumbnails = {}
GetThumbnails.OnClientEvent:Connect(function(thumbnailsList)
	for _, thumbnail in pairs(thumbnailsList) do
		thumbnails[thumbnail[1]] = {
			thumbnail[2], -- TextureId
			thumbnail[3], -- Level
			thumbnail[4], -- Rarity
			thumbnail[5], -- Desc
			thumbnail[6] -- Class
		}
	end
	thumbnailsReceived = true
end)
GetThumbnails:FireServer()

local rarityColors = nil
local rarityNames = nil
local function getRarityColor(rarityNum)
	if rarityColors == nil then
		repeat wait() until RS:FindFirstChild("RarityColors") ~= nil
		rarityColors = RS.RarityColors:GetChildren()
	end
	for _, color in pairs(rarityColors) do
		if tonumber(string.sub(color.Name, 1, 1)) == rarityNum then
			return color.Value
		end
	end
end
local function getRarityName(rarityNum)
	for _, color in pairs(rarityColors) do
		if tonumber(string.sub(color.Name, 1, 1)) == rarityNum then
			return string.sub(color.Name, 3)
		end
	end
end

local categories = {{}, {}, {}, {}, {}, {}, {}} -- Storage here

local generating = false
local generateInfo = TweenInfo.new(.1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local function generateInventory(categoryNum, arrowButton)
	if thumbnailsReceived == false then
		repeat wait() until thumbnailsReceived == true
	end
	if generating == true then
		repeat wait() until generating == false
	end
	generating = true
	for tabNum = 1, 6 do
		local tab = inventoryGui.Inventory.Tabs.Half.TabArea[tostring(tabNum) .. "-" .. CATEGORY_NAMES[tabNum]]
		if tabNum == categoryNum then
			local colorShow = TS:Create(tab, generateInfo, {ImageColor3 = Color3.new(1, 1, 1)})
			colorShow:Play()
			tab.Parent.Parent.Line.Underline:TweenPosition(UDim2.new(.167 * (tabNum - 1), 0, 0, 0),
				Enum.EasingDirection.Out, Enum.EasingStyle.Quad, .1, true)
			tab.Parent.Title.Text = CATEGORY_NAMES[tabNum]
		else
			local colorFade = TS:Create(tab, generateInfo, {ImageColor3 = Color3.fromRGB(52, 53, 61)})
			colorFade:Play()
		end
	end
	if arrowButton ~= nil then
		spawn(function()
			arrowButton.Icon:TweenSizeAndPosition(
				UDim2.new(.8, 0, .8, 0),
				UDim2.new(.1, 0, .1, 0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				.05,
				true
			)
			wait(.05)
			arrowButton.Icon:TweenSizeAndPosition(
				UDim2.new(.9, 0, .9, 0),
				UDim2.new(.05, 0, .05, 0),
				Enum.EasingDirection.In,
				Enum.EasingStyle.Linear,
				.05,
				true
			)
		end)
	end
	for slotNum = 1, #slots - 1 do
		local slot = slots[1].Parent[tostring(slotNum)]
		local slotInfo = categories[categoryNum][slotNum]
		if slotInfo ~= nil then -- Slot exists
			local thumbnailId = thumbnails[slotInfo[1]]
			if #thumbnailId[1] > 0 then -- Thumbnail exists
				slot.Button.Image = thumbnailId[1]
				slot.Text.Visible = false
			else -- Thumbnail does not exist
				slot.Button.Image = ""
				slot.Text.Text = slotInfo[1]
				slot.Text.Visible = true
			end
			slot.Amount.Text = slotInfo[2]
			slot.Amount.Visible = true
			slot.Button.BackgroundColor3 = getRarityColor(thumbnailId[3])
		else -- Slot does not exist
			slot.Text.Visible = false
			slot.Button.Image = ""
			slot.Amount.Visible = false
			slot.Button.BackgroundColor3 = Color3.fromRGB(39, 38, 48)
		end		
	end
	
	-- generate toolbar
	for slotNum = 1, 3 do
		if slotNum == categories[7][1] then
			toolbar["Slot" .. tostring(slotNum)].UIStroke.Color = Color3.new(1, 1, 1)
		else
			toolbar["Slot" .. tostring(slotNum)].UIStroke.Color = Color3.fromRGB(64, 64, 72)
		end
		local info = categories[7][slotNum + 1]
		if info ~= nil then
			toolbar["Slot" .. tostring(slotNum)].Image = thumbnails[info[1]][1]
			toolbar["Slot" .. tostring(slotNum)].BackgroundColor3 = getRarityColor(thumbnails[info[1]][3])
			if #thumbnails[info[1]][1] <= 0 then
				toolbar["Slot" .. tostring(slotNum)].Text.Visible = true
				toolbar["Slot" .. tostring(slotNum)].Text.Text = info[1]
			else
				toolbar["Slot" .. tostring(slotNum)].Text.Visible = false					
			end
		else
			toolbar["Slot" .. tostring(slotNum)].Image = ""
			toolbar["Slot" .. tostring(slotNum)].Text.Visible = false
			toolbar["Slot" .. tostring(slotNum)].BackgroundColor3 = Color3.fromRGB(39, 38, 48)
		end
	end
	generating = false
end

local currentCategory = 1

local died = false
local fade = inventoryGui.Fade
local fadeInfo = TweenInfo.new(FADE_TIME, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local fadeIn = TS:Create(fade, fadeInfo, {BackgroundTransparency = 0})
local fadeOut = TS:Create(fade, fadeInfo, {BackgroundTransparency = 1})

local function setGuiClone(character)
	died = false
	local humanoid = character:WaitForChild("Humanoid")
	character:WaitForChild("Head")
	if not character.Head:FindFirstChildOfClass("Decal") then
		repeat wait() until character.Head:FindFirstChildOfClass("Decal") ~= nil
	end
	character.Archivable = true
	local clone = character:Clone()
	character.Archivable = false
	clone:SetPrimaryPartCFrame(workspace.InventoryArea.DummyAnchor.CFrame)
	clone.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	clone.Parent = workspace.InventoryArea
	local idle1 = clone.Humanoid:LoadAnimation(script.Idle1)
	local idle2 = clone.Humanoid:LoadAnimation(script.Idle2)
	local currentAnim = idle1
	
	character:WaitForChild("Humanoid").Died:Connect(function()
		died = true
		if inventoryGui.Inventory.Visible == true then
			fadeIn:Play()
			wait(FADE_TIME)
			clone:Destroy()
			inventoryGui.Inventory.Visible = false
			inventoryGui.Header.Visible = false
			toolbar.Parent.DescShow.Visible = false
			script.Parent.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			script.Parent.Text = "Open Inventory"
			local camera = workspace.CurrentCamera

			camera.CameraType = Enum.CameraType.Custom
			if player.Character == nil then
				player.CharacterAdded:wait() -- If the player has died, wait for respawn
				repeat wait() until player.Character:FindFirstChild("Humanoid") ~= nil

			end
			camera.CameraSubject = player.Character.Humanoid	
			fadeOut:Play()
		end
	end)

	local count = 0
	spawn(function()
		currentAnim:Play()
		while wait(4) do
			if not character then
				break
			end
			currentAnim:Stop()
			if count >= 4 then
				currentAnim = idle2
				count = 1
			else
				currentAnim = idle1
				count = count + 1
			end
			currentAnim:Play()
		end
	end)
end
if player.Character then
	setGuiClone(player.Character)
end
player.CharacterAdded:Connect(function(character)
	setGuiClone(character)
end)

Inventory.OnClientEvent:Connect(function(inventory)
	local melee = inventory[1]
	local ranged = inventory[2]
	local shield = inventory[3]
	local armor = inventory[4]
	local food = inventory[5]
	local misc = inventory[6]
	local toolbar = inventory[7]
	
	for categoryNum, category in pairs({melee, ranged, shield, armor, food, misc, toolbar}) do
		table.clear(categories[categoryNum])
		for _, item in pairs(category) do
			table.insert(categories[categoryNum], item)
		end
	end
	
	generateInventory(currentCategory) -- LEFT OFF HERE
end)

local debounce = false
script.Parent.MouseButton1Up:Connect(function()
	if debounce == false and died == false then
		debounce = true
		fadeIn:Play()
		wait(FADE_TIME)
		script.Parent.AutoButtonColor = false
		
		local camera = workspace.CurrentCamera
		if inventoryGui.Inventory.Visible == false then -- Opening
			inventoryGui.Inventory.Visible = true
			inventoryGui.Header.Visible = true
			toolbar.Parent.DescShow.Visible = true
			script.Parent.BackgroundColor3 = Color3.fromRGB(170, 0, 0)
			script.Parent.Text = "Close Inventory"
			
			camera.CameraType = Enum.CameraType.Scriptable
			camera.CFrame = workspace.InventoryArea.CameraPos.CFrame
		else -- Closing
			inventoryGui.Inventory.Visible = false
			inventoryGui.Header.Visible = false
			toolbar.Parent.DescShow.Visible = false
			script.Parent.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
			script.Parent.Text = "Open Inventory"
			
			camera.CameraType = Enum.CameraType.Custom
			if player.Character == nil then
				player.CharacterAdded:wait() -- If the player has died, wait for respawn
				repeat wait() until player.Character:FindFirstChild("Humanoid") ~= nil
				
			end
			camera.CameraSubject = player.Character.Humanoid
		end
		
		fadeOut:Play()
		wait(FADE_TIME)
		script.Parent.AutoButtonColor = true
		debounce = false
	end
end)

local toolDebounce = false
local function slotToggle(slotNum)
	if toolDebounce == false then
		toolDebounce = true
		Toolbar:FireServer(slotNum, inventoryGui.Inventory.Visible)
		wait(.3)
		toolDebounce = false
	end
end

local mouse = player:GetMouse()
local guiFollow = nil
local fromToolbar = false
local function showDesc(slotNum, categoryNum)
	if slotNum == nil then
		toolbar.Parent.DescShow.Desc.Visible = false
	else
		local slotInfo
		if not categoryNum then
			slotInfo = categories[currentCategory][slotNum]
		else
			slotInfo = categories[categoryNum][slotNum + 1]
		end
		if slotInfo ~= nil then
			local itemInfo = thumbnails[slotInfo[1]]
			local level = itemInfo[2]
			local rarity = itemInfo[3]
			local desc = itemInfo[4]
			local class = itemInfo[5]
			toolbar.Parent.DescShow.Desc.Title.Text = slotInfo[1]
			if currentCategory < 5 then
				toolbar.Parent.DescShow.Desc.Type.Text = "Level " .. tostring(level) .. " " .. 
					getRarityName(rarity) .. " " .. class
			elseif currentCategory == 5 then
				toolbar.Parent.DescShow.Desc.Type.Text = getRarityName(rarity) .. " resource"
			else
				toolbar.Parent.DescShow.Desc.Type.Text = getRarityName(rarity) .. " item"
			end
			
			toolbar.Parent.DescShow.Desc.Desc.Text = desc
			toolbar.Parent.DescShow.Desc.Desc.TextSize = 
				math.ceil(toolbar.Parent.DescShow.Desc.AbsoluteSize.Y / 8)

			if rarity < 1 then
				toolbar.Parent.DescShow.Desc.Title.TextColor3 = Color3.new(1, 1, 1)
				toolbar.Parent.DescShow.Desc.Type.TextColor3 = Color3.new(1, 1, 1)
			else
				toolbar.Parent.DescShow.Desc.Title.TextColor3 = getRarityColor(rarity)
				toolbar.Parent.DescShow.Desc.Type.TextColor3 = getRarityColor(rarity)
			end
			local frame = toolbar.Parent.DescShow
			if guiFollow == nil then
				guiFollow = true
				Run.RenderStepped:Connect(function()
					if inventoryGui.Inventory.Visible == true then
						if fromToolbar == true then
							frame.Position = UDim2.new(
								0, 
								-inventoryGui.AbsoluteSize.X - frame.AbsoluteSize.X + mouse.X,
								0,
								-inventoryGui.AbsoluteSize.Y - frame.AbsoluteSize.Y + mouse.Y
							)
							frame.Desc.Position = UDim2.new(-3.3, 0, 0, 0)
						else
							frame.Position = UDim2.new(
								0,
								-inventoryGui.AbsoluteSize.X + mouse.X,
								0,
								-inventoryGui.AbsoluteSize.Y + mouse.Y
							)
							frame.Desc.Position = UDim2.new(.4, 0, 0, 0)
						end
					end
				end)
			end
			toolbar.Parent.DescShow.Desc.Visible = true
		else
			toolbar.Parent.DescShow.Desc.Visible = false
		end
	end
end

for slotNum = 1, 3 do
	toolbar["Slot" .. tostring(slotNum)].MouseButton1Up:Connect(function()
		slotToggle(slotNum)
	end)
	toolbar["Slot" .. tostring(slotNum)].MouseEnter:Connect(function()
		fromToolbar = true
		showDesc(slotNum, 7)
	end)
end
toolbar.MouseLeave:Connect(function()
	showDesc()
end)

for slotNum = 1, 20 do
	local slot = slots[1].Parent[tostring(slotNum)]
	slot.Button.MouseButton1Up:Connect(function()
		if categories[currentCategory][slotNum] ~= nil then
			Inventory:FireServer(currentCategory, slotNum)
		end
	end)
	slot.MouseEnter:Connect(function()
		fromToolbar = false
		showDesc(slotNum)
	end)
end
slots[1].Parent.MouseLeave:Connect(function()
	showDesc()
end)

UIS.InputBegan:Connect(function(input, gpe)
	if not gpe and input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.One then
			slotToggle(1)
		elseif input.KeyCode == Enum.KeyCode.Two then
			slotToggle(2)
		elseif input.KeyCode == Enum.KeyCode.Three then
			slotToggle(3)
		end
	end
end)

local tabDebounce = false
local leftArrow = slots[1].Parent.Parent.Left
local rightArrow = slots[1].Parent.Parent.Right
leftArrow.MouseButton1Up:Connect(function()
	if tabDebounce == false then
		tabDebounce = true
		currentCategory -= 1
		if currentCategory <= 0 then
			currentCategory = 6
		end
		generateInventory(currentCategory, leftArrow)
		wait(.1)
		tabDebounce = false
	end
end)
rightArrow.MouseButton1Up:Connect(function()
	if tabDebounce == false then
		tabDebounce = true
		currentCategory += 1
		if currentCategory > 6 then
			currentCategory = 1
		end
		generateInventory(currentCategory, rightArrow)
		wait(.1)
		tabDebounce = false
	end	
end)
for tabNum = 1, 6 do
	local tab = inventoryGui.Inventory.Tabs.Half.TabArea[tostring(tabNum) .. "-" .. CATEGORY_NAMES[tabNum]]
	tab.MouseButton1Up:Connect(function()
		if tabDebounce == false then
			tabDebounce = true
			currentCategory = tabNum
			generateInventory(currentCategory)
			wait(.1)
			tabDebounce = false
		end
	end)
end

BreakResource.OnClientEvent:Connect(function(resourceName, health, maxHealth)
	local resource = workspace.Resources:FindFirstChild(resourceName)
	local char = player.Character
	if resource and char then
		local dist = (resource.PrimaryPart.Position - char.HumanoidRootPart.Position).Magnitude
		if dist < 30 then
			if resource.PrimaryPart:FindFirstChild("ResourceHealth") then
				resource.PrimaryPart.ResourceHealth:Destroy()
			end
			local healthBar = script.ResourceHealth:Clone()
			healthBar.Back.Front.Size = UDim2.new(health / maxHealth, 0, 1, 0)
			healthBar.Parent = resource.PrimaryPart
			healthBar.Enabled = true
			
			local shakeStart = tick()
			local original = resource.PrimaryPart.CFrame
			local particle = script.Hit.HitParticle:Clone()
			particle.Parent = resource.PrimaryPart
			particle.Color = ColorSequence.new(resource.PrimaryPart.Color:Lerp(Color3.new(0), .2))
			particle.Enabled = true
			shake = Run.RenderStepped:Connect(function()
				local diff = tick() - shakeStart
				if diff >= .5 then
					shake:Disconnect()
					particle:Destroy()
				elseif particle.Enabled == true and diff >= .35 then
					particle.Enabled = false
				end
				local new = original * CFrame.Angles(math.sin(math.pi * diff * 10) / 20, 0, 0)
				resource:SetPrimaryPartCFrame(new)
			end)
		end
	end
end)

No errors in the output or anything.

try adding print statements in your script to debug and trim your script to where the problem occurs cause i assure you no ones reading all that

1 Like

The problem is within this code right here

if player.Character then
	setGuiClone(player.Character)
end
player.CharacterAdded:Connect(function(character)
	setGuiClone(character)
end)

If i remove this portion of the code then it works but the image of my custom character does not pop up in my inventory GUI

1 Like

Nvm im dumb. I fixed it. Simple mistakes are the worst.

1 Like