local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- Create ScreenGui
local screenGui = Instance.new("ScreenGui")
screenGui.Name = "MinecraftStyleInventory"
screenGui.Parent = playerGui
screenGui.Enabled = false

-- Main frame (bigger and darker)
local mainFrame = Instance.new("Frame")
mainFrame.Size = UDim2.new(0, 700, 0, 450)
mainFrame.Position = UDim2.new(0.5, -350, 0.4, -225)
mainFrame.BackgroundColor3 = Color3.fromRGB(30, 30, 30)
mainFrame.BorderSizePixel = 0
mainFrame.Parent = screenGui

local uICorner = Instance.new("UICorner")
uICorner.CornerRadius = UDim.new(0, 15)
uICorner.Parent = mainFrame

-- ViewportFrame (left side) for character display
local viewport = Instance.new("ViewportFrame")
viewport.Size = UDim2.new(0, 280, 0, 420)
viewport.Position = UDim2.new(0, 10, 0, 15)
viewport.BackgroundColor3 = Color3.fromRGB(60, 60, 60)
viewport.BorderSizePixel = 0
viewport.Parent = mainFrame

local vpCorner = Instance.new("UICorner")
vpCorner.CornerRadius = UDim.new(0, 12)
vpCorner.Parent = viewport

-- Crafting grid 2x2 (top right)
local craftingFrame = Instance.new("Frame")
craftingFrame.Size = UDim2.new(0, 200, 0, 200)
craftingFrame.Position = UDim2.new(0, 310, 0, 15)
craftingFrame.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
craftingFrame.BorderSizePixel = 0
craftingFrame.Parent = mainFrame

local craftCorner = Instance.new("UICorner")
craftCorner.CornerRadius = UDim.new(0, 10)
craftCorner.Parent = craftingFrame

local craftingGrid = Instance.new("UIGridLayout")
craftingGrid.CellSize = UDim2.new(0, 90, 0, 90)
craftingGrid.CellPadding = UDim2.new(0, 10, 0, 10)
craftingGrid.FillDirection = Enum.FillDirection.Horizontal
craftingGrid.Parent = craftingFrame

-- Create 4 crafting slots (empty frames)
local craftingSlots = {}
for i=1,4 do
	local slot = Instance.new("Frame")
	slot.Name = "CraftSlot"..i
	slot.BackgroundColor3 = Color3.fromRGB(80, 80, 80)
	slot.BorderSizePixel = 2
	slot.BorderColor3 = Color3.fromRGB(150, 150, 150)
	slot.Parent = craftingFrame

	local corner = Instance.new("UICorner")
	corner.CornerRadius = UDim.new(0, 6)
	corner.Parent = slot

	-- Placeholder text label
	local label = Instance.new("TextLabel")
	label.Text = ""
	label.BackgroundTransparency = 1
	label.Size = UDim2.new(1, 0, 1, 0)
	label.TextColor3 = Color3.new(1, 1, 1)
	label.Font = Enum.Font.SourceSansBold
	label.TextScaled = true
	label.Parent = slot

	craftingSlots[i] = slot
end

-- Inventory frame (bottom right)
local inventoryFrame = Instance.new("Frame")
inventoryFrame.Size = UDim2.new(0, 380, 0, 210)
inventoryFrame.Position = UDim2.new(0, 310, 0, 230)
inventoryFrame.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
inventoryFrame.BorderSizePixel = 0
inventoryFrame.Parent = mainFrame

local invCorner = Instance.new("UICorner")
invCorner.CornerRadius = UDim.new(0, 12)
invCorner.Parent = inventoryFrame

local inventoryGrid = Instance.new("UIGridLayout")
inventoryGrid.CellSize = UDim2.new(0, 80, 0, 50)
inventoryGrid.CellPadding = UDim2.new(0, 10, 0, 10)
inventoryGrid.FillDirection = Enum.FillDirection.Horizontal
inventoryGrid.Parent = inventoryFrame

-- Equipped tool display
local equippedFrame = Instance.new("Frame")
equippedFrame.Size = UDim2.new(0, 680, 0, 40)
equippedFrame.Position = UDim2.new(0, 10, 0, 400)
equippedFrame.BackgroundColor3 = Color3.fromRGB(70, 70, 30)
equippedFrame.BorderSizePixel = 2
equippedFrame.BorderColor3 = Color3.fromRGB(255, 255, 120)
equippedFrame.Parent = mainFrame

local equippedLabel = Instance.new("TextLabel")
equippedLabel.Size = UDim2.new(1, 0, 1, 0)
equippedLabel.BackgroundTransparency = 1
equippedLabel.Text = "Equipped Tool: None"
equippedLabel.TextColor3 = Color3.new(1, 1, 1)
equippedLabel.Font = Enum.Font.SourceSansBold
equippedLabel.TextScaled = true
equippedLabel.Parent = equippedFrame

-- Helper: setup viewport to show player character
local function setupViewport()
	if not player.Character then return end
	viewport:ClearAllChildren()

	local charClone = player.Character:Clone()

	-- Make parts anchored & remove Motor6D for static pose
	for _, part in ipairs(charClone:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Anchored = true
			part.CanCollide = false
		elseif part:IsA("Motor6D") then
			part:Destroy()
		end
	end

	local rootPart = charClone:FindFirstChild("HumanoidRootPart") or charClone:FindFirstChildWhichIsA("BasePart")
	if rootPart then
		local offset = rootPart.CFrame
		for _, part in ipairs(charClone:GetDescendants()) do
			if part:IsA("BasePart") then
				part.CFrame = part.CFrame - offset.p
			end
		end
	end

	charClone.Parent = viewport

	-- Lighting for character
	local light = Instance.new("PointLight")
	light.Brightness = 2
	light.Range = 15
	light.Parent = rootPart

	-- Setup camera to look at character nicely
	local camera = Instance.new("Camera")
	viewport.CurrentCamera = camera
	camera.Parent = viewport

	camera.CFrame = CFrame.new(Vector3.new(0, 2, 5), Vector3.new(0, 1, 0)) 
end

-- Refresh inventory: show tools in backpack as buttons
local function refreshInventory()
	print("Refreshing inventory...")
	-- Clear previous buttons
	for _, child in ipairs(inventoryFrame:GetChildren()) do
		if child:IsA("TextButton") then
			child:Destroy()
		end
	end

	inventoryGrid.Parent = inventoryFrame

	for _, tool in ipairs(player.Backpack:GetChildren()) do
		print("Found tool:", tool.Name)
		if tool:IsA("Tool") then
			local btn = Instance.new("TextButton")
			btn.Size = UDim2.new(0, 80, 0, 50)
			btn.BackgroundColor3 = Color3.fromRGB(90, 90, 90)
			btn.BorderSizePixel = 0
			btn.TextColor3 = Color3.new(1,1,1)
			btn.Font = Enum.Font.SourceSansBold
			btn.TextScaled = true
			btn.Text = tool.Name
			btn.Parent = inventoryFrame

			btn.MouseButton1Click:Connect(function()
				equippedLabel.Text = "Equipped Tool: " .. tool.Name
				if player.Character and player.Character:FindFirstChildOfClass("Humanoid") then
					player.Character.Humanoid:EquipTool(tool)
				end
			end)
		end
	end

	-- Show equipped tool
	if player.Character then
		for _, child in ipairs(player.Character:GetChildren()) do
			if child:IsA("Tool") then
				equippedLabel.Text = "Equipped Tool: " .. child.Name
			end
		end
	end
	print("Inventory refreshed.")
end

-- Toggle GUI with E key
UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.E then
		screenGui.Enabled = not screenGui.Enabled
		if screenGui.Enabled then
			setupViewport()
			refreshInventory()
		end
	end
end)

-- Update inventory when tools change
player.Backpack.ChildAdded:Connect(refreshInventory)
player.Backpack.ChildRemoved:Connect(refreshInventory)

player.CharacterAdded:Connect(function(char)
	char.ChildAdded:Connect(function(child)
		if child:IsA("Tool") then
			refreshInventory()
		end
	end)
	char.ChildRemoved:Connect(function(child)
		if child:IsA("Tool") then
			refreshInventory()
		end
	end)
	refreshInventory()
end)

if player.Character then
	refreshInventory()
end

-- Initial refresh if GUI already enabled (usually off)
if screenGui.Enabled then
	setupViewport()
end

can you fix the viewport it not showing the character i try it to 10 days and it not working