How can I make a player lose all their items in their inventory when they die?

Im making a survival game, and I am trying to figure out how a player can lose their inventory items when they die. I have tried using a local script that goes in the players inventory, but that ended out with the player keeping their inventory items. This is the following local script:


script.Parent = game.Players.LocalPlayer

Game:GetService(“Value”)

local Value = playerInventory

playerInventory.Value = nil

player.Inventory.itemValue = game:GetService(“Value”)

local Value = #Items

when Player.Died:Connect(function(ClearInventory)

when ClearInventory = true then

playerInventory.Value = 0

ClearInventory.Value = 0

else
Player.Alive = ClearInventory = false do

ClearInventory = false

    end
end)

When the player died, the inventory items still stay in the players inventory. This is hard to do because this needs to be done in order to make the player lose stuff, like in survival games when you die you lose items. I do not know on how this is supposed to make the player lose their stuff, but I need help with this because the game would seem more boring without the physic.

2 Likes

There are some simple tutorials you can follow online. I know you might feel like that is just being lazy, but, in reality, as long as you: read through the script until you understand it, type it yourself etc. it’s usually considered okay amongst most developers, because that way you actually learn something like you would by doing it yourself.

i dont know if this will work

local debounce = true

game.Players.PlayerAdded:Connect(function(p)
  p.CharacterAdded:Connect(function(char)
	if debounce == true then
		local backpack = p:FindFirstChildOfClass("Backpack")
		game.ServerStorage.Tool:Clone().Parent = backpack
		debounce = false
	end
	
 end)

end)

Player is not a Humanoid instance, therefore it cannot “die”. Instead, use this instead.

local humanoid
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        humanoid = character:WaitForChild("Humanoid")
    end)
end)

You put the variable before anything else, and then define it when the player’s character is added and the humanoid is found. You can then proceed to use the humanoid anywhere else like as if it were a global variable.

humanoid.Died:Connect(function()
    --Died event here
end)

If you are confused, let me sum it up here.

While you defined the variable “Player” as LocalPlayer, you however did not define it as character, nor a humanoid. And since it is not a humanoid instance it therefore does not have a “Died” event. What you should do in order to be able to use the local variable anywhere else is define the variable at the start of the script, give it its value as the humanoid when the character is added (like shown above), and then you can proceed to do anything else.

I would also like to let you know that I don’t think there is a Player.Alive event or a “when” statement. I think this is what you want.

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
script.Parent = character

humanoid.Died:Connect(function()
    for _, item in ipairs(character:GetChildren()) do
        if item:IsA("ObjectValue") then --replace objectvalue with what the value is
            item:Destroy()
        else
            print("Player has no items!")
        end
    end
end)

Basically what I did up there was get the local player, then get the character of it or wait for it, then wait for the humanoid. If you don’t know why, it’s because when a player joins a game (it also depends on their internet connection) most of the time certain parts of their character haven’t even loaded, which is why we use the method :WaitForChild to wait for the humanoid. It yields the whole thread like the regular wait(), and the whole script won’t run until it has found the humanoid.

The next thing I did up there was use the humanoid.Died event. Wherein I ran a loop through the player’s character to see if there were any ObjectValues (or whatever they are). If the item was an ObjectValue, they would then be destroyed.

Let me know if I helped!

1 Like

Where does that script go? Like are all those lines of code in 1 script? I have no clue on how it can find the players inventory whenever the player dies. My mind is completely shuffled with that post, therefore I am misunderstanding if all the lines of code you wrote goes in a local or normal script.

You need to run a while loop when they die to check when they respawn, when they do, you clear their inventory:

  local player = game.Players.LocalPlayer
    local char = game.Workspace:WaitForChild(player.Name) -- Waiting for player to load in
    local hum = char.Humanoid

    hum.Died:Connect(function()
    	while wait() do -- Constantly checking if player respawns
    		if hum.Health == 100 then -- Waiting for player to respawn
    			player.Backpack:ClearAllChildren()
    			break -- We delete the players' inventory once they respawn and break out of the while loop
    		else
    			player.Backpack:ClearAllChildren()
    			-- We delete the players' inventory when they die, but still check if they respawn
    		end
    	end
    end)

I put this in a local script, inside of the starter player scripts folder which is inside of the starter player, I’ve tested it and it works.

2 Likes

I’d like to remind you that general scripts can in no way access LocalPlayer. It’s a LocalScript, supposed to be. And should be parented to the character. Although, if you put the local script in StarterCharacterScripts you won’t need t owait for the player’s character to be added or the humanoid. You can just use the dot syntax. Any scripts put in StarterCharacterScripts are parented to the character and run once the character has fully loaded.

Also, another thing I’d like to suggest. Please open the output window and check if there are any errors when this script is ran. If there are, please post it here.

This is a bad idea, you should not keep looping just to keep checking if the player has respawned. It’s like using RunService’s RenderStepped for nonexpensive actions like customizing the camera. If your script is structured well, you can avoid using "repeat wait()"s or while wait() dos.

I see what you mean but it is the best thing I thought of as when I tested it without the while loop, the player would lose their inventory when they died, but get it back once they re-spawned, which is then when I decided to use a while loop; it still does work though.

You should handle this on a server script, but I won’t interfere.

Player is a object, not a humanoid. You need to use Humanoid.Died.