Hello everyone! Im DragonDarkVader!
*Credits to: The owner of the Picture
When i join or reset a Pistol Automatically Equips itself
And here’s the code:
-- Place inside StarterPlayerScripts
--// Custom system INTEGRATED with default Roblox Tools //--
local Players = game:GetService("Players")
local StarterGui = game:GetService("StarterGui")
local UserInputService = game:GetService("UserInputService")
local Player = Players.LocalPlayer
local Tools = {}
local Keybinds = {
[Enum.KeyCode.One] = 1,
[Enum.KeyCode.Two] = 2,
[Enum.KeyCode.Three] = 3,
[Enum.KeyCode.Four] = 4,
[Enum.KeyCode.Five] = 5,
[Enum.KeyCode.Six] = 6,
[Enum.KeyCode.Seven] = 7,
[Enum.KeyCode.Eight] = 8,
[Enum.KeyCode.Nine] = 9,
[Enum.KeyCode.Zero] = 10,
}
--// Initializer //--
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
--// Input Handler //--
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if Player.Character == nil then
return
end
local humanoid = Player.Character:FindFirstChild("Humanoid")
if humanoid == nil then
return
end
-- Handles 0-9 keycodes
if input.UserInputType == Enum.UserInputType.Keyboard then
if Keybinds[input.KeyCode] then
local slot = Keybinds[input.KeyCode]
local existingTool = Player.Character:FindFirstChildOfClass("Tool")
if existingTool then
if Tools[slot] == existingTool then
humanoid:UnequipTools()
else
if Tools[slot] then
humanoid:EquipTool(Tools[slot])
end
end
else
if Tools[slot] then
humanoid:EquipTool(Tools[slot])
end
end
end
end
end)
UserInputService.InputChanged:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then
return
end
if Player.Character == nil then
return
end
local humanoid = Player.Character:FindFirstChild("Humanoid")
if humanoid == nil then
return
end
-- Handles mouse wheel
if input.UserInputType == Enum.UserInputType.MouseWheel then
local direction = input.Position.Z > 0 and 1 or -1
local existingTool = Player.Character:FindFirstChildOfClass("Tool")
if existingTool then
local toolSlot = table.find(Tools, existingTool)
if toolSlot then
local toSlot = toolSlot + direction
if toSlot < 1 then
toSlot = #Tools
elseif toSlot > #Tools then
toSlot = 1
end
humanoid:EquipTool(Tools[toSlot])
end
end
end
end)
--// Tools Handler //--
local function AddToolToList(tool)
if tool:IsA("Tool") then
if not table.find(Tools, tool) then
table.insert(Tools, tool)
end
end
end
local function RemoveToolFromList(tool)
if tool:IsA("Tool") then
if tool.Parent ~= Player.Character then -- Check if tool has been forcefully removed
local toolIndex = table.find(Tools, tool)
if toolIndex then
table.remove(Tools, toolIndex)
end
end
end
end
Player.ChildAdded:Connect(function(backpack)
if backpack:IsA("Backpack") then
local character = Player.Character or Player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid", 5)
if humanoid then
-- In case any new tools gets added
local addedConnection = backpack.ChildAdded:Connect(function(tool)
AddToolToList(tool)
end)
-- In case any new tools gets destroyed
local removedConnection = backpack.ChildRemoved:Connect(function(tool)
RemoveToolFromList(tool)
end)
-- Add already-existing tools
for _, tool in ipairs(backpack:GetChildren()) do
AddToolToList(tool)
end
-- Clear upon death
local deadConnection
deadConnection = humanoid.Died:Connect(function()
addedConnection:Disconnect()
removedConnection:Disconnect()
deadConnection:Disconnect()
Tools = {}
end)
end
end
end)
Credits to the owner of the Script too