So im working on a custom hotbar system to go with my inventory system, and theres a couple of things i want to achieve that i dont know how to
- Hide the normal roblox hotbar & Inventory
- Create keybinds to match the number of the weapon
- Change all weapon Numbers when a tool is removed (example: tools in slot1 slot2 slot3, tool 2 is removed, i want slot3 to change to slot 2)
- (Maybe) if it isnt to hard find a way to get rid of guns that were sent back into the inventory, that were already equipped
Videos of how it works / some bugs
Just a quick video on how it works, also gets the 3 point in action
(The hotbar numbers dont change on removal)
Current Code and Stuff
Creating the folders for players
game.Players.PlayerAdded:Connect(function(plr)
local c = Instance.new("Folder", game.ReplicatedStorage.PlayerInvetorys)
c.Name = plr.Name .. "Folder"
local d = Instance.new("Folder", c)
d.Name = "Inventory"
local e = Instance.new("Folder", c)
e.Name = "Hotbar"
end)
Main Script stuff is fired to
function GetRidOfTools(plr, char)
if char:FindFirstChildWhichIsA("Tool") then
char:FindFirstChildWhichIsA("Tool"):Destroy()
end
for i, v in ipairs(plr.Backpack:GetChildren()) do
v:Destroy()
end
end
game.ReplicatedStorage.TotallyCoolEvents.EquipGun.OnServerEvent:Connect(function(plr, d, c)
local Char = plr.Character
if c == "ToHotBar" then
print("SendingToHotBar")
if not d then return end
if not Char then return end
if not d:IsA("Tool") then return end
local hbNum = #game.ReplicatedStorage.PlayerInvetorys:FindFirstChild(plr.Name .. "Folder"):WaitForChild("Hotbar"):GetChildren()
if hbNum >= 8 then return "InvFull" end
d.Parent = game.ReplicatedStorage.PlayerInvetorys:FindFirstChild(plr.Name .. "Folder"):WaitForChild("Hotbar")
end
if c == "ToInv" then
if not d then return end
if not Char then return end
if not d:IsA("Tool") then return end
d.Parent = game.ReplicatedStorage.PlayerInvetorys:FindFirstChild(plr.Name .. "Folder"):WaitForChild("Inventory")
end
if c == "EquipGun" then
if not d then return end
if not Char then return end
if not d:IsA("Tool") then return end
if Char:FindFirstChildWhichIsA("Tool") or plr.Backpack:FindFirstChildWhichIsA("Tool") then
GetRidOfTools(plr, Char)
local TrueGun = d:Clone()
TrueGun.Parent = Char
end
end
end)
Inventory Code
local player = game.Players.LocalPlayer
local mainFolder = game.ReplicatedStorage.PlayerInvetorys:WaitForChild(player.Name .. "Folder")
local inv = mainFolder:WaitForChild("Inventory")
local hb = mainFolder:WaitForChild("Hotbar")
local CurrentInv = inv:GetChildren()
local C = game.ReplicatedStorage.ItemFrame
local hoverinv = script.Parent.Parent.HoverInfo
local mouse = player:GetMouse()
local InventoryFrames = {}
print(CurrentInv)
function equipitem(item, frame)
if not item then warn("Wheres My Gun At?") return end
hoverinv.Visible = false
local D = game.ReplicatedStorage.TotallyCoolEvents.EquipGun:FireServer(item, "ToHotBar")
if D ~= nil then
print(D)
end
end
local function Hover(item)
hoverinv.Visible = true
if item:FindFirstChild("strings") then
hoverinv.GunName.Text = item.Name
hoverinv.ExpNumber.Text = item.strings.CurrentExp.Value .. " / " .. item.strings.ReqExp.Value
hoverinv.LvlNumber.Text = item.strings.GunLevel.Value
hoverinv.GunDmg.Text = item.strings.Damage.Value
hoverinv.GunRPM.Text = item.strings.FireRate.Value
hoverinv.GunAmmo.Text = item.strings.Ammo.Value .. " / " .. item.strings.StoredAmmo.Value
hoverinv.Tal1.Text = item.strings.T1.Value .. " | Unlocks @ Level " .. item.strings.ReqT1.Value
hoverinv.Tal2.Text = item.strings.T2.Value .. " | Unlocks @ Level " .. item.strings.ReqT2.Value
hoverinv.Tal3.Text = item.strings.T3.Value .. " | Unlocks @ Level " .. item.strings.ReqT3.Value
hoverinv.Tal4.Text = item.strings.T4.Value .. " | Unlocks @ Level " .. item.strings.ReqT4.Value
end
end
function Creation(item)
local K = C:Clone()
InventoryFrames[item] = K
K.Text = item.Name
K.Visible = true
K.Parent = script.Parent.ISF
K.MouseButton1Click:Connect(function()
print(InventoryFrames)
equipitem(item, K)
end)
K.MouseEnter:Connect(function()
Hover(item)
end)
K.MouseLeave:Connect(function()
hoverinv.Visible = false
end)
end
function Deletion(item)
local frame = InventoryFrames[item]
frame:Destroy()
InventoryFrames[item] = nil
end
for i, v in ipairs(CurrentInv) do
Creation(v)
end
inv.ChildAdded:Connect(function(Child)
Creation(Child)
end)
inv.ChildRemoved:Connect(function(Child)
Deletion(Child)
end)
mouse.Move:Connect(function()
if script.Parent.Visible == true then
hoverinv.Position = UDim2.new(0,mouse.X,0,mouse.Y)
end
end)
Hotbar Code (Basically used the inventory code but on the hotbar)
local player = game.Players.LocalPlayer
local mainFolder = game.ReplicatedStorage.PlayerInvetorys:WaitForChild(player.Name .. "Folder")
local inv = mainFolder:WaitForChild("Inventory")
local hb = mainFolder:WaitForChild("Hotbar")
local CurrentInv = inv:GetChildren()
local C = game.ReplicatedStorage["HotBarSlot#"]
local hoverinv = script.Parent.Parent.HoverInfo
local mouse = player:GetMouse()
local HotbarFrames = {}
print(CurrentInv)
function unequipitem(item, frame)
if not item then warn("Wheres My Gun At?") return end
hoverinv.Visible = false
local D = game.ReplicatedStorage.TotallyCoolEvents.EquipGun:FireServer(item, "ToInv")
if D ~= nil then
print(D)
end
end
function equipgun(item)
if not item then warn("Wheres My Gun At?") return end
hoverinv.Visible = false
local D = game.ReplicatedStorage.TotallyCoolEvents.EquipGun:FireServer(item, "EquipGun")
if D ~= nil then
print(D)
end
end
local function Hover(item)
hoverinv.Visible = true
if item:FindFirstChild("strings") then
hoverinv.GunName.Text = item.Name
hoverinv.ExpNumber.Text = item.strings.CurrentExp.Value .. " / " .. item.strings.ReqExp.Value
hoverinv.LvlNumber.Text = item.strings.GunLevel.Value
hoverinv.GunDmg.Text = item.strings.Damage.Value
hoverinv.GunRPM.Text = item.strings.FireRate.Value
hoverinv.GunAmmo.Text = item.strings.Ammo.Value .. " / " .. item.strings.StoredAmmo.Value
hoverinv.Tal1.Text = item.strings.T1.Value .. " | Unlocks @ Level " .. item.strings.ReqT1.Value
hoverinv.Tal2.Text = item.strings.T2.Value .. " | Unlocks @ Level " .. item.strings.ReqT2.Value
hoverinv.Tal3.Text = item.strings.T3.Value .. " | Unlocks @ Level " .. item.strings.ReqT3.Value
hoverinv.Tal4.Text = item.strings.T4.Value .. " | Unlocks @ Level " .. item.strings.ReqT4.Value
end
end
function Creation(item)
local K = C:Clone()
local D = #hb:GetChildren()
K.Name = "HotBarSlot#" .. D
K.Number.Text = D
HotbarFrames[item] = K
K.Text = item.Name
K.Visible = true
K.Parent = script.Parent
K.MouseButton1Click:Connect(function()
if player.PlayerGui.Main.InvFrame.Visible == true then
unequipitem(item, K)
else
equipgun(item)
end
end)
K.MouseEnter:Connect(function()
if player.PlayerGui.Main.InvFrame.Visible == true then
Hover(item)
end
end)
K.MouseLeave:Connect(function()
hoverinv.Visible = false
end)
end
function Deletion(item)
local frame = HotbarFrames[item]
frame:Destroy()
HotbarFrames[item] = nil
end
for i, v in ipairs(hb:GetChildren()) do
Creation(v)
end
hb.ChildAdded:Connect(function(Child)
Creation(Child)
end)
hb.ChildRemoved:Connect(function(Child)
Deletion(Child)
end)
The Structure of the gui
(Extra notes)
The main frames are cloned from replicated storage
Extra Stuff that i would love answered
Any idea how to make the gun not fire when the mouse is in a gui?
Heres the gun firing code
RunService.Stepped:Connect(function()
if UserInputService:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
if not Tool.Parent:IsA("Backpack") or Tool.Parent:IsA("Folder") then
if script.Parent.Strings.Ammo.Value >= 1 then
local mPos = ms.Hit.Position
FEvent:FireServer(mPos)
end
end
end
end)
Also since im using fastcast, is there a way to get the bullets to not freeze in the air and break when a gun is destroyed
(As shown in the video)