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?
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