Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How could I make a player "passively" hold an item?

So I made a lantern model and I intend to make the player hold this lantern automatically without the player having to click on the icon in his backpack or press 1. I basically want the lantern to be a part of the player. Would a way of doing this be to disable to backpack GUI?

3 answers

✓ Accepted answer

Hello, iLordy!

I made the script you want, it works perfectly!

This script don't need to disable the backpack gui =D

Code:

--Local Script--
ToolName = "Lantern" -- Change to the lantern(or tool) name

plr = game.Players.LocalPlayer
repeat wait() until plr.Character

local Equipped = nil

-- First Equip the Lantern(If is in player inventory)
if plr.Backpack:FindFirstChild(ToolName) and plr.Backpack:FindFirstChild(ToolName):IsA("Tool") then
	plr.Character.Humanoid:EquipTool(plr.Backpack:FindFirstChild(ToolName))
end


-- Equips when player isn't using any tools
plr.Backpack.ChildAdded:Connect(function(Item)
	wait(.1)
	if Equipped ~= nil and Equipped == Item then
		if plr.Backpack:FindFirstChild(ToolName) and plr.Backpack:FindFirstChild(ToolName):IsA("Tool") then
			wait(.1)
			plr.Character.Humanoid:EquipTool(plr.Backpack:FindFirstChild(ToolName))
		end
	end
end)


--Detects when player equipped a tool, and saves it on a var
plr.Character.ChildAdded:Connect(function(Item)
	if Item:IsA("Tool") then
		Equipped = Item
	end
end)

Good Luck with your Games

You can disable the backpack by doing this:

local starterGui = game:GetService"StarterGui"

starterGui:SetCoreGuiEnabled(
    Enum.CoreGuiType.Backpack,
    false
)

To keep the tool equipped:

local plr = game:GetService("Players").LocalPlayer
local char = game.Workspace:WaitForChild(plr.Name)

while wait() do
    char.Humanoid:EquipTool(plr.Backpack.Lantern)
end

There is a handy function in the Humanoid object called :EquipTool(Tool). When this function is run with a tool as the parameter, the character that the Humanoid is attached to will equip the specified tool. A way to automatically cause this behavior to happen is to place the Tool into the player's backpack, and then use the function.

Here's a script that you can put into StarterCharacterScripts, assuming the lantern name is "Lantern" and it's already in the player's backpack.

local player = game.Players.LocalPlayer -- Get the player.
local backpack = player:WaitForChild("Backpack") -- Get the backpack.
local character = player.Character or player.CharacterAdded:wait() -- Get the character.
local humanoid = character:WaitForChild("Humanoid") -- Get the Humanoid.

local tool = backpack:FindFirstChild("Lantern") -- Returns True or False.
if tool ~= true then -- If there is no tool called Lantern:
	return -- Stop the script.
end

humanoid:EquipTool(tool) -- Equip the tool using the function.

I hope this helped and if so, please accept it! cya

3 comments

0

line 6 FindFirstChild doesn’t return bool.

0

I just noticed that; it actually returned a reference to an Instance. However, it can be treated as a boolean in if statements.

0

I may have misread the question as well...

This discussion is preserved as it was. New answers, comments, and votes are closed.