local Players = game:GetService("Players")
local UIS = game:GetService("UserInputService")
local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local gui = script.Parent
local frame = gui:WaitForChild("InventoryFrame")
local template = frame:WaitForChild("ItemTemplate")
template.Visible = false
local opened = false
local function tools()
local t = {}
for _, v in ipairs(backpack:GetChildren()) do
if v:IsA("Tool") then t[#t + 1] = v end
end
local char = player.Character
if char then
for _, v in ipairs(char:GetChildren()) do
if v:IsA("Tool") then t[#t + 1] = v end
end
end
return t
end
local function equipToggle(tool)
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:FindFirstChildOfClass("Humanoid")
if not hum or not tool then return end
if tool.Parent == char then hum:UnequipTools() else hum:EquipTool(tool) end
end
local function makePreview(button, tool)
local vp = button:FindFirstChild("Preview") or Instance.new("ViewportFrame")
vp.Name, vp.Parent = "Preview", button
vp.Size, vp.Position = UDim2.new(1, -8, 1, -30), UDim2.new(0, 4, 0, 4)
vp.BackgroundTransparency, vp.BorderSizePixel = 0.2, 0
for _, c in ipairs(vp:GetChildren()) do c:Destroy() end
local h = tool:FindFirstChild("Handle")
if not (h and h:IsA("BasePart")) then return end
local wm = Instance.new("WorldModel", vp)
local p = h:Clone(); p.Anchored = true; p.CanCollide = false; p.CFrame = CFrame.new(); p.Parent = wm
local cam = Instance.new("Camera", vp); vp.CurrentCamera = cam
local s = math.max(p.Size.X, p.Size.Y, p.Size.Z)
local d = math.max(3, s * 2.2)
cam.CFrame = CFrame.new(d, d * 0.7, d, 0, 0, 0)
end
local function refresh()
for _, c in ipairs(frame:GetChildren()) do
if c:IsA("TextButton") and c ~= template then c:Destroy() end
end
for _, tool in ipairs(tools()) do
local b = template:Clone()
b.Visible, b.Text, b.Name, b.Parent = true, "", tool.Name .. "_Btn", frame
b.Size = UDim2.new(0, 120, 0, 120)
local name = b:FindFirstChild("ItemName") or Instance.new("TextLabel")
name.Name, name.Parent = "ItemName", b
name.Size, name.Position = UDim2.new(1, -8, 0, 22), UDim2.new(0, 4, 1, -24)
name.BackgroundTransparency, name.TextScaled, name.Text = 1, true, tool.Name
makePreview(b, tool)
b.MouseButton1Click:Connect(function() equipToggle(tool); refresh() end)
end
end
UIS.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.M then
opened = not opened
frame.Visible = opened
if opened then refresh() end
elseif input.KeyCode == Enum.KeyCode.Q then
local hum = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
if hum then hum:UnequipTools(); if opened then refresh() end end
end
end)
local function bindChar(char)
char.ChildAdded:Connect(function(c) if opened and c:IsA("Tool") then refresh() end end)
char.ChildRemoved:Connect(function(c) if opened and c:IsA("Tool") then refresh() end end)
end
backpack.ChildAdded:Connect(function(c) if opened and c:IsA("Tool") then refresh() end end)
backpack.ChildRemoved:Connect(function(c) if opened and c:IsA("Tool") then refresh() end end)
if player.Character then bindChar(player.Character) end
player.CharacterAdded:Connect(function(char) bindChar(char); if opened then refresh() end end)