Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
iLordy 27
6 years ago

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

Log in to vote
2
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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

Ad
Log in to vote
0
Answered by 6 years ago

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
Log in to vote
-1
Answered by 6 years ago

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

0
line 6 FindFirstChild doesn’t return bool. User#19524 175 — 6y
0
I just noticed that; it actually returned a reference to an Instance. However, it can be treated as a boolean in if statements. Cowation 50 — 5y
0
I may have misread the question as well... Cowation 50 — 5y

Answer this question