Editing a Script that can automatically equips a Pistol when Joining/Reset

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

1 Like

Can anybody help? Pleaseeee i really need it.

Simply do a server check and connect the Player:CharacterAdded(char) function and then equip the tool with Humanoid:EquipTool(tool)

Or in a local script: add a script that only equips the gun with the method mentioned before, without any event connections, just equip the tool and place that script in StarterCharacterScripts

What does that mean? I am not a Coder guy hehehe

--Place on ServerScripts
local plrs = game:GetService("Players")
local item = game:GetService("ReplicatedStorage"):WaitForChild(" place here name of item ")

plrs.PlayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function(char)
      local hum = char:WaitForChild("Humanoid")
      local newitem = item:Clone()
      newitem.Parent = plr.Backpack
      hum:EquipTool(newitem)
   end
end)
1 Like

OOF, I got a lot of Errors when placing the script.

Can you show me? (character limit)

Also replace the :EquipTool = item to :EquipTool(item)

1 Like

Ok here you go:
Error2

It still has an Error (Characters limit)

Place the tool in replicated storage
then recopy the script above (i modified it).
change the value inside the WaitForChild

1 Like

Do i need to put it inside a Folder?

sure but change that to

if you want to put it on a folder:

 local item = game:GetService("ReplicatedStorage"):WaitForChild(" place here the folder name  "):FindFirstChild(" item name here ")

You forgot to do :Connect(function(char))

Also please, before you start making games, learn basics first.

hum:EquipTool = Geist17 isn’t valid either.
https://developer.roblox.com/en-us/api-reference/function/Humanoid/EquipTool

3 Likes

Where do i put the first script in: Humanoid:EquipTool

Nevermind, I figured it out. I just need to put in in StarterCharacterScripts