I’m trying to make a simple inventory that hovers whenever your mouse is hovering it, and equips and unequips when you click on it. For some reason, my script glitches a lot, so i need help on how i am supposed to do it. Here’s my attempt:
local RS = game:GetService("RunService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local StarterGui = game:GetService("StarterGui")
local TweenService = game:GetService("TweenService")
local UIs = game:GetService("UserInputService")
local TI = TweenInfo.new(.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
-- Variables
local Player = game.Players.LocalPlayer
local Backpack: Backpack = Player:WaitForChild("Backpack")
local Frame = script.Parent
local InvFrame = Frame:WaitForChild("Inventory")
local Templates = Frame:WaitForChild("Templates")
local InventoryTemplate = Templates:WaitForChild("InventoryTemplate")
local WorldModels = ReplicatedStorage:WaitForChild("WorldModels")
local CurrentTools = 0
local Module3d = require(ReplicatedStorage:WaitForChild("Module3D"))
local EquipDebounce = false
function Hover(Template, On)
local Tween1 = TweenService:Create(Template, TI, {Size = UDim2.new(0.204, 0,0.823, 0)})
local Tween2 = TweenService:Create(Template, TI, {Size = UDim2.new(0.126, 0,0.614, 0)})
if On then
Tween1:Play()
else
Tween2:Play()
end
end
function EquipFunction(Template)
if EquipDebounce then return end
EquipDebounce = true
local Tool = Template.Tool.Value
print("press")
if Tool ~= nil then
print("equipped tool")
if CheckifToolExists(Tool) then
ReplicatedStorage.Tool:FireServer("Unequip", Tool)
Hover(Template, false)
else
game.ReplicatedStorage.Tool:FireServer("Equip", Tool)
end
task.wait(.8)
EquipDebounce = false
end
end
function AddTool(ToolName)
print(ToolName)
local WM = WorldModels:FindFirstChild(ToolName)
if not WM then return false end
local Template = InventoryTemplate:Clone()
local camera = Instance.new("Camera")
local vpf = Module3d:Attach3D(Template, WM)
camera.Parent = Template
vpf.Visible = true
CurrentTools += 1
Template.Name = ToolName
Template.Visible = true
Template.Tool.Value = ToolName
Template.Parent = InvFrame
Template.Number.Text = CurrentTools
Template:SetAttribute("Row", CurrentTools)
local Equip = Template.Equip
-- Services --
Equip.MouseEnter:Connect(function()
Hover(Template, true)
end)
Equip.MouseLeave:Connect(function()
Hover(Template, false)
end)
Equip.MouseButton1Click:Connect(function()
EquipFunction(Template)
end)
end
function PutToolInBackpack(Tool)
Tool.Parent = Player:WaitForChild("Backpack")
end
function CheckifToolExists(ToolName)
if Player.Character:FindFirstChild(ToolName) then
return true
else
return false
end
end
function RemoveTool(ToolName)
if InvFrame:FindFirstChild(ToolName) then
InvFrame:FindFirstChild(ToolName):Destroy()
CurrentTools -= 1
end
end
Backpack.ChildAdded:Connect(function(Tool)
if InvFrame:FindFirstChild(Tool.Name) then
return
end
print("tool added")
AddTool(Tool.Name)
end)
Backpack.ChildRemoved:Connect(function(Tool)
if not Player.Character:FindFirstChild(Tool) then
RemoveTool(Tool.Name)
end
end)
Player.Character.ChildRemoved:Connect(function(Child)
if Child:IsA("Tool") and Child.Parent ~= Player.Backpack then
RemoveTool(Child.Name)
end
end)
local CurrentEquipped = nil
UIs.InputBegan:Connect(function(Input, IsChatting)
if EquipDebounce then return end
if IsChatting then return end
local KeyCode = Input.KeyCode.Name
if KeyCode == "One" then KeyCode = 1 end
if KeyCode == "Two" then KeyCode = 2 end
if KeyCode == "Three" then KeyCode = 3 end
if KeyCode == "Four" then KeyCode = 4 end
for _, frame in pairs(InvFrame:GetChildren()) do
if frame:IsA("Frame") then
local attribute = frame:GetAttribute("Row")
if attribute == KeyCode then
if CurrentEquipped ~= frame then
Hover(frame, true)
if CurrentEquipped ~= nil then
Hover(CurrentEquipped, false)
end
end
CurrentEquipped = frame
EquipFunction(frame)
end
end
end
end)
Thank you for your help.