Help! Items with 'Job' Bool Keep Disappearing on Death

Hey everyone,

I’m running into a weird problem with my game and could really use some help. My game saves player data and restores it when they respawn, but items that have a BoolValue named ‘Job’ are still disappearing when a player dies. I thought these items were supposed to stick around, but nope!

What’s Going On:

When a player dies, any items in their inventory with a BoolValue named ‘Job’ should be preserved. Instead, they’re being destroyed. I’m trying to figure out why this is happening.

Here’s the Code I’m Using:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            humanoid.Died:Connect(function()
                local inventory = player:FindFirstChild("Inventory")
                if inventory then
                    savePlayerData(player)
                    for _, item in pairs(inventory:GetChildren()) do
                        if item:IsA("BoolValue") then
                            local keep = false
                            for _, child in pairs(item:GetChildren()) do
                                if child.Name == "Job" then
                                    keep = true
                                    break
                                end
                            end
                            if not keep then
                                item:Destroy()
                            end
                        end
                    end
                end
            end)
        end
    end)
end)

Items are found in the player’s BackPack.
line 6: local inventory = player:FindFirstChild("Backpack")

Try using print(“test”) at each step, then look at the output.
I can’t test on my own because I don’t see what inventory you’re talking about.

Thanks for the suggestion! I’ll add print statements to debug the issue. Here’s a simplified version of the script where I focus on the critical sections:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDataStore")

local function getItemsList(folder)
    local itemsList = {}
    if folder then
        for _, item in pairs(folder:GetChildren()) do
            table.insert(itemsList, item.Name)
        end
    end
    return itemsList
end

local function loadPlayerData(player)
    local success, data = pcall(function()
        return playerDataStore:GetAsync(player.UserId)
    end)
    if not success or data == nil then
        warn("Error loading data for " .. player.Name .. ": " .. tostring(data))
        return {
            bananas = 0,
            visa = false,
            Inventory = {},
            Armors = {}
        }
    end
    return data
end

local function setupPlayerFolders(player, data)
    local inventory = player:FindFirstChild("Inventory") or Instance.new("Folder")
    inventory.Name = "Inventory"
    inventory.Parent = player

    local armors = player:FindFirstChild("Armors") or Instance.new("Folder")
    armors.Name = "Armors"
    armors.Parent = player

    print("Inventory and Armors folders set up for:", player.Name)

    if not player:GetAttribute("DataLoaded") then
        if data.Inventory then
            for _, weaponName in pairs(data.Inventory) do
                print("Processing weapon:", weaponName)
                local weaponValue = Instance.new("BoolValue")
                weaponValue.Name = weaponName
                weaponValue.Parent = inventory

                local item = ReplicatedStorage.Items:FindFirstChild(weaponName)
                if item then
                    local clone = item:Clone()
                    clone.Parent = player.Backpack
                else
                    warn("Item not found in ReplicatedStorage.Items:", weaponName)
                end
            end
        else
            print("No inventory data found for player:", player.Name)
        end

        if data.Armors then
            for _, armorName in pairs(data.Armors) do
                print("Processing armor:", armorName)
                local armorValue = Instance.new("BoolValue")
                armorValue.Name = armorName
                armorValue.Parent = armors
            end
        else
            print("No armor data found for player:", player.Name)
        end

        player:SetAttribute("DataLoaded", true)
    end
end

local function savePlayerData(player)
    local leaderstats = player:FindFirstChild("leaderstats")
    local inventory = player:FindFirstChild("Inventory")
    local armors = player:FindFirstChild("Armors")

    if not leaderstats or not leaderstats:FindFirstChild("Bananas") then
        warn("Bananas value or leaderstats folder not found for player:", player.Name)
        return
    end

    local bananas = leaderstats.Bananas.Value
    local visa = leaderstats.VISA.Value

    local success, err = pcall(function()
        playerDataStore:SetAsync(player.UserId, {
            bananas = bananas,
            visa = visa,
            Inventory = getItemsList(inventory),
            Armors = getItemsList(armors)
        })
        print("Data saved successfully for:", player.Name)
    end)

    if not success then
        warn("Error saving data for " .. player.Name .. ": " .. tostring(err))
    end
end

game.Players.PlayerAdded:Connect(function(player)
    print("Player added:", player.Name)
    wait(1)
    local data = loadPlayerData(player)
    setupPlayerFolders(player, data)

    player.CharacterAdded:Connect(function(character)
        print("Character added for player:", player.Name)
        local humanoid = character:FindFirstChildOfClass("Humanoid")
        if humanoid then
            humanoid.Died:Connect(function()
                print("Player died:", player.Name)
                local inventory = player:FindFirstChild("Inventory")
                if inventory then
                    savePlayerData(player)
                    for _, item in pairs(player.Backpack:GetChildren()) do
                        if not item:FindFirstChild("Job") then
                            item:Destroy()
                        end
                    end
                end
            end)
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(player)
    print("Player removing:", player.Name)
    savePlayerData(player)
end)

From what I see, the player needs to dead 1 time for the “character” to be detected on line 114.
The object must also not be held in hand.
With this, I created a “Job” “stringvalue” in my tool, this one is detected but the object disappears. (i use google translates srry)

Is it relevant that your code only considers BoolValues when destroying items?

You could also replace these lines with

local keep = item:FindFirstChild("Job") ~= nil

to simplify your code.

line 112, currently: player.CharacterAdded:Connect(function(character)

changes to this: local character = player.Character

To fix the problem where you have to die once to detect the character

are these prints running?
a possibility i see is that the issue is being caused by how you check for the humanoid in the character added function. The fact that you use FindFirstChildOfClass means that if the humanoid hasn’t yet loaded, no connection will be made. A way to fix is potential issue is to use WaitForChild rather than FindFirstChildOfClass

Thanks everyone!

Hello!

I want to express my gratitude for your patience and support while I resolved the data management issue in my game. Thanks to your suggestions and help, I was able to find an effective solution.

How I Fixed the Problem:

I decided to create two separate folders within the data system: one for deletable objects and another for non-deletable objects. This separation has allowed me to manage the data more effectively, ensuring that important objects are not accidentally deleted and that deletable objects are handled appropriately.