So I’m following this https://www.youtube.com/watch?v=aBDf7zQRJqU&t=239s&ab_channel=TheRobloxCoach tuturial by the roblox coach for a inventory system, then i encounted a few bugs
and heres my code
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local UserInputService = game:GetService(“UserInputService”)
local TweenService = game:GetService(“TweenService”)
local ViewportUtil = require(script.ViewportUtil)
local PropAssets = ReplicatedStorage.Assets.Items.Prop
local Player = Players.LocalPlayer
local PlayerGui = Player.PlayerGui
local Mouse = Player:GetMouse()
local BackpackGui = PlayerGui:WaitForChild(“HUD”)
local Hotbar = BackpackGui.Inventory
local Default = Hotbar.Default
local GeneratedIcons = true
local MaxNormalSlots = 5
local MaxReservedSlot = 3
Hotbar:SetAttribute(“NormalMax”, MaxNormalSlots)
Hotbar:SetAttribute(“ReservedMax”, MaxReservedSlot)
local White = Color3.new(1, 1, 1)
local Gold = Color3.new(1, 1, 0)
local Black = Color3.new(0, 0, 0)
local TweenData = {
[“Pop”] = TweenInfo.new(0.3, Enum.EasingStyle.Back),
[“Long”] = TweenInfo.new(0.2)
}
local OpagueWhiteImage = {ImageTransparency = 0, ImageColor3 = White}
local OpagueWhiteFrame = {BackgroundTransparency = 0, BackgroundColor3 = White}
local OpagueGold = {BackgroundColor3 = Gold}
local TranslucentBlack = {ImageTransparency = 0.5, ImageColor3 = Black}
local SlotCreated = {Size = UDim2.fromScale(1, 1)}
local Backpack = nil
local Character = nil
local Humanoid = nil
local Slots = table.create(MaxNormalSlots + MaxReservedSlot)
local Tools = table.create(MaxNormalSlots + MaxReservedSlot)
local Locked = false
local Equipped = nil
local LastTool = nil
local Left, Right = nil, nil
local MouseListener = nil
local function ValidTool(x)
local Slot = Tools[x.Name]
local Reserved = x:GetAttribute(“Reserved”)
local NormalCount = Hotbar:GetAttribute(“NormalCount”)
local ReservedCount = Hotbar:GetAttribute(“ReservedCount”)
if not x:IsA("Tool") or (Slot and x == Slot.Tool) then return false end
if (Slot and x ~= Slot.Tool) or (not slot and (not Reserved and NormalCount == MaxNormalSlots) or (Reserved and ReservedCount == MaxReservedSlot)) then
task.wait()
if x.Parent == Character and Equipped then
Humanoid:EquipTool(Equipped.Tool)
end
x:Destory()
return false
end
return true
end
local function SortSlots()
for i, v in ipairs(Slots) do
v.Index = v.Frame.LayoutOrder
v.Frame.hotkey.TextLabel.Text = v.Index
end
table.sort(Slots, function(a, b)
return a.Index < b.Index
end)
end
local function FinalizeLayout(Input)
if not Input or (Input and Input.UserInputType == Enum.UserInputType.MouseButton1) then
if MouseListener and MouseListener.Connected then
MouseListener:Disconnect()
SortSlots()
end
end
end
local function CreateSlot(x)
if x:IsA(“Humanoid”) and x.Parent == Character then
Humanoid = x
return
end
if not ValidTool(x) then return end
local Slot = {}
table.insert(Slots, Slot)
Slot.Index = #Slots
Slot.Tool = x
Tools[x.Name] = Slot
Slot.Frame = Default:Clone()
Slot.Frame.Visible = true
local ToolModel = PropAssets:FindFirstChild(x.Name)
if GeneratedIcons and ToolModel then
Slot.Frame.TextLabel:Destroy()
Slot.Frame.Icon:Destroy()
local Viewport = ViewportUtil.Create(ToolModel[x.Name], "Tool")
Viewport.AnchorPoint = Vector2.new(0.5, 0.5)
Viewport.Position = UDim2.fromScale(0.5, 0.5)
Viewport.Size = UDim2.fromScale(1.5, 1.5)
Viewport.Parent = Slot.Frame
elseif x.TextureId ~= "" then
Slot.Frame.TextLabel:Destroy()
Slot.Frame.Icon.Image = x.TextureId
else
Slot.Frame.Icon:Destroy()
Slot.Frame.TextLabel.Text = string.sub(x.Name, 1, 2)
end
Slot.Frame.Name = x.Name
Slot.Frame.Hotkey.TextLabel.Text = Slot.Index
Slot.Frame.LayoutOrder = Slot.Index
Slot.Frame.Parent = Hotbar
TweenService:Create(Slot.Frame, TweenData.Pop, SortCreated):Play()
if x:GetAttribute("Reserved") then
Hotbar:SetAttribute("ReservedCount", Hotbar:GetAttribute("NormalCount") + 1)
else
Hotbar:SetAttribute("NormalCount", Hotbar:GetAttribute("NormalCount") + 1)
end
Slot.Frame.MouseButton1Down:Connect(function()
Slot:Toggle()
end)
local function MouseMoved(Input)
if Input.UserInputType == Enum.UserInputType.MouseMovement then
if Left and Input.Position.X < Left.Frame.AbsolutePosition.X + Left.Frame.Absolute.X then
Slot:Shift(Left)
Right = Left ~= Slot and Left or Slots[Slot.Index + 1]
Left = Slots[Right.Index - (Right.Index - 1 ~= Slot.Index and 1 or 2)]
elseif Right and Input.Position.X > Right.Frame.AbsolutePositon.X then
Slot:Shift(Right)
Left = Right ~= Slot and right or Slot[Slot.Index - 1]
Right = Slots[Left.Index + (Left.Index + 1 ~= Slot.Index and 1 or 2)]
end
end
end
Slot.Frame.MouseButton1Down:Connect(function()
if #Slot <= 1 then return end
FinalizeLayout()
Left = Slot[Slot.Index - 1]
Right = Slot[Slot.Index + 1]
MouseListener = UserInputService.InputChanged:Connect(MouseMoved)
end)
function Slot:Toggle()
if (Locked and self == Equipped) or Humanoid.Health <= 0 then return end
if Equipped then
TweenService:Create(Equipped.Frame, TweenData.Long, TranslucentBlack):Play()
TweenService:Create(Equipped.Frame.Hotkey, TweenData.Long, OpagueWhiteFrame):Play()
if self == Equipped then
Equipped = nil
if self.Tool.Parent == Character then
Humanoid:UnequipTools()
end
return
end
end
if self.Tool.Parent == Backpack then
Humanoid.EquipTool(self.Tool)
end
TweenService:Create(self.Frame, TweenData.Long, OpagueWhiteImage):Play()
TweenService:Create(self.Frame.Hotkey, TweenData.Long, OpagueGold):Play()
Equipped = self
end
function Slot:Shift(Target)
if Target then
self.Frame.LayoutOrder, Target.Frame.LayoutOrder = Target.Fram.LayoutOrder, self.Frame.LayoutOrder
return
end
self.Index -= 1
self.Frame.Hotkey.TextLabel.Text = self.Index
self.Frame.LayoutOrder = self.Index
end
end
function Slot:Delete()
if not Player.Parent then return end
table.remove(Slots, self.Index)
Tools[self.Tool.Name] = nil
self.Frame:Destory()
if self == Equipped then
Equipped = nil
end
local Size = #Slots
for i = self.Index, Size do
Slots[i]:Shift()
end
if self.Reserved then
Hotbar:SetAttribute("ReservedCount", Hotbar:GetAttribute("ReservedCount") - 1)
else
Hotbar:SetAttribute("NormalCount", Hotbar:GetAttribute("NormalCount") - 1)
end
if x.Parent == Character then
Slots:Toggle()
end
return Slot
end
local function Select(Input, Event)
if Event then return end
local Slot = Slots[Input.KeyCode.Value - 48]
if Slot then
Slot:Toggle()
end
end
local function RemovedSlot(x)
if not x:IsA("Tool") then return end
local Slot = Tools[x.Name]
if Slot and x == Slot.Tool then
Mouse.Icon = ""
if Slot == Equipped and x.Parent == Backpack then
Slot:Toggle()
elseif x.Parent ~= Backpack and x.Parent ~= Character then
Slot:Delete()
end
end
end
local function SetTransparency(Object, Alpha, IgnoreList, TweenData)
local Properties = {
[“Frame”] = “BackgroundTransparency”,
[“ViewportFrame”] = “ImageTransparency”,
[“ScrollingFrame”] = “ScrollBarImageTransparency”,
[“ImageButton”] = “ImageTransparency”,
[“ImageLabel”] = “ImageTransparency”,
[“TextButton”] = “TextTransparency”,
[“TextLabel”] = “TextTransparency”,
[“TextBox”] = “TextTransparency”
}
for i, v in ipairs(Object:GetDescendants()) do
if not v:IsA("GuiObject") or (IgnoreList and table.find(IgnoreList, v)) then continue end
if TweenData then
TweenService:Create(v, TweenData, {[Properties[v.ClassName]] = Alpha}):Play()
if v:IsA("ScrollingFrame") or v:IsA("TextButton") then
TweenService:Create(v, TweenData, {BackgroundTransparency = Alpha}):Play()
end
else
v[Properties[v.ClassName]] = Alpha
if v:IsA("ScrollingFrame") or v:IsA("TextButton") then
v.BackgroundTransparency = Alpha
end
end
end
end
local function SetHotbarTransparency(Alpha, KeepEquipped)
for _, Frame in ipairs(Hotbar:GetChildren()) do
if not Frame:IsA(“GuiObject”) or (KeepEquipped and Equipped and Frame == Equipped.Frame) then continue end
TweenService:Create(Frame, TweenData.Long, {ImageTransparency = (0.5 * Alpha) + 0.5}):Play()
SetTransparency(Frame, Alpha, nil, TweenData.Long)
end
end
local function LockHotbar(State, KeepEquipped)
if State then
if not KeepEquipped and Equipped then
Humanoid:UnequipTools()
end
Locked = true
SetHotbarTransparency(0.5, KeepEquipped)
else
Locked = false
SetHotbarTransparency(0, false)
end
if LastTool and not Equipped then
local Slot = Tools[LastTool]
if Slot then
Slot:Toggle()
end
end
end
script.SetState.Event:Connect(LockHotbar)
local function CharacterAdded(x)
FinalizeLayout()
for i = #Slots, 1, -1 do
local Slot = Slots[i]
if Slot then
Slot:Delete()
end
end
Mouse.Icon = ""
Character = x
LastTool = nil
x.ChildAdded:Connect(CreateSlot)
x.ChildRemoved:Connect(RemoveSlot)
for i, v in ipairs(x:GetChildren()) do
CreateSlot(v)
end
Backpack = Player:WaitForChild("Backpack")
Backpack.ChildAdded:Connect(CreateSlot)
Backpack.ChildRemoved:Connect(RemoveSlot)
for i, v in ipairs(Backpack:GetChildren()) do
CreateSlot(v)
end
end
UserInputService.InputBegan:Connect(Select)
UserInputService.InputEnded:Connect(FinalizeLayout)
Player.CharacterAdded:Connect(CharacterAdded)
if Player.Character(Player.Character) then
CharacterAdded(Players.Character)
end
return true
my local script called client
local Players = game:GetService(“Players”)
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local StarterGui = game:GetService(“StarterGui”)
local CollectionService = game:GetService(“CollectionService”)
local player = Players.LocalPlayer
local playerGui = player:WaitForChild(“PlayerGui”)
local START = {}
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
for _, module in ipairs(script:GetChildren()) do
require(script.Parent.Client.BackPack)
end
and a moduel script
local Module = {}
local Orgin = CFrame.new()
local White = Color3.new(1,1,1)
local LightDirection= Vector3.new(1,1,1)
local function UpdateCamera(Size, Center, Camera)
local MaxSize = math.sqrt(Size.X^2 + Size.Y^2 + Size.Z^2)
local Depth = MaxSize / math.tan(math.rad(Camera.FieldOfView))
Camera.CFrame = CFrame.new(Center.X, Center.Y, Center.Z - Depth) * CFrame.Angles(0, math.pi, 0)
end
function Module.Create(Object)
assert(Object:IsA(“BasePart”) or Object:IsA(“Model”), string.format(“BasePart or Model expected, got %s”), Object.ClassName)
Object = Object:Clone()
local Frame = Instance.new("ViewportFrame")
local Camera = Instance.new("Camera")
local Size = nil
local Center = nil
if Object:IsA("BasePart") then
Object:ClearAllChildren()
Object.CFrame = Object.CFrame * CFrame.Angles(0, 0, 0)
Size = Object.Size
Center = Object.CFrame.Position
else
Object:PivotTo(Object:GetPivot() * CFrame.Angles(0, 0, 0))
Size = Object:GetExtentsSize()
Center = Object:GetBoundingBox()
end
Camera.FieldOfView = 20
UpdateCamera(Size, Center, Camera)
Object.Parent = Frame
Camera.Parent = Frame
Frame.Ambient = White
Frame.LightColor = White
Frame.LightDirection = LightDirection
Frame.BackgroundTransparency = 1
Frame.SizeConstraint = (Size.X > Size.Y) and Enum.SizeConstraint.RelativeXX or Enum.SizeConstraint.RelativeYY
Frame.CurrentCamera = Camera
return Frame
end
I’m honestly super confused about these code i just started coding and I honestly have no clue what i’m doing I watched match tuturial before, and many beginner and intermediate series , but I don’t really know what to do all the time.