How to disable backpack on death and enable on respawn?

How to make disable backpack on death, and enable on respawn.

local humanoid = script.Parent:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	game:GetService('StarterGui'):SetCoreGuiEnabled('Backpack', false)--disable backpack
	humanoid:UnequipTools()--unequip tools
end)
1 Like

What is your use case for this? Most tools already don’t work upon death and will be removed from your backpack if you select and then deselect them while dead.

Are you trying to make the tools stay in the users inventory? If so, then this won’t work. Instead, try putting the tools in the StarterPack or, alternatively, insert all the tools in the user’s Backpack and Character in a table, unparent them, and then reparent them to the user’s Backpack when they respawn.

I dont know how to fix that: Untitled Game Roblox Studio 2022 02 15 21 55 39 - Sendvid

local StarterGui = game:GetService("StarterGui");
local humanoid = script.Parent:WaitForChild("Humanoid");

function onSpawn()
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true);
end
onSpawn();

function onDeath()
	humanoid:UnequipTools();
	StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false);
end
humanoid.Died:Connect(onDeath);

The onSpawn function will run when the character spawns.

1 Like

if the script is inside the player character then just do this

local humanoid = script.Parent:WaitForChild("Humanoid")

game:GetService('StarterGui'):SetCoreGuiEnabled('Backpack', true)--enables it when player respawns backpack

humanoid.Died:Connect(function()
	game:GetService('StarterGui'):SetCoreGuiEnabled('Backpack', false)--disable backpack
	humanoid:UnequipTools()--unequip tools
end)
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			humanoid:UnequipTools()
			for _, tool in ipairs(player.Backpack:GetChildren()) do
				tool:Destroy()
			end
		end)
	end)
end)

You can proxy achieve this without disabling the player’s backpack, you’d do this by unequipping any tool equipped by the player’s character and then iterating over the player’s backpack and destroying all of their tools.

Do this, it should work.

local humanoid = script.Parent:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	game:GetService('StarterGui'):SetCoreGuiEnabled('Backpack', false)--disable backpack
	humanoid:UnequipTools()--unequip tools
    repeat wait(0.1) until humanoid.Health = 100
    game:GetService('StarterGui'):SetCoreGuiEnabled('Backpack', true)--enable backpack
end)