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

Trying to add to IntValue on player death isn't working, and returning error?

Asked by 3 years ago

Hi so basically, I'm trying to update a value on the player leaderstats when the player dies, but my script keeps returning this error: "ServerScriptService.PlrManager:13: attempt to index nil with 'FindFirstDescendant'". I know what this is, I just don't know what's causing it, because everything looks right.

Here is my code to help:

game.Players.PlayerAdded:Connect(function(Player)
    local Container = Instance.new("Folder", Player)
    Container.Name = "leaderstats"

    local CurrentStage = Instance.new("IntValue", Container)
    local Tokens = Instance.new("IntValue", Container)
    local Deaths = Instance.new("IntValue", Container)
    CurrentStage.Name = "Stage"
    Tokens.Name = "Tokens"
    Deaths.Name = "Deaths"

    --below this is what's causing errors
    local PIW = game.Workspace:FindFirstChild(Player.Name)
    local Hum = PIW:FindFirstDescendant("Humanoid")
    Hum.OnDied:Connect(function(onDeath)
        Deaths.Value = Deaths.Value + 1
    end)
end)
0
You don't need to do FindFirstDescendant, because the Humanoid isn't a descendant of the player. Use FindFirstChild instead. gamernight687 138 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

The Humanoid.OnDied Event is not a valid member, use Humanoid.Died and use Player.CharacterAdded to make the Humanoid.Died Event work.

game.Players.PlayerAdded:Connect(function(Player)
    local Container = Instance.new("Folder", Player)
    Container.Name = "leaderstats"

    local CurrentStage = Instance.new("IntValue", Container)
    local Tokens = Instance.new("IntValue", Container)
    local Deaths = Instance.new("IntValue", Container)
    CurrentStage.Name = "Stage"
    Tokens.Name = "Tokens"
    Deaths.Name = "Deaths"
    local function onCharacterAdded(char) -- Create a function when a character is spawned
        local Humanoid = char:WaitForChild("Humanoid") -- Wait for the Humanoid object to load.
        Humanoid.Died:Connect(function(...) -- Connect a function to the Humanoid.Died Event, we cannot use Humanoid.OnDied, it's not a thing.
            Deaths.Value = Deaths.Value + 1 -- Add a death to the Player List
        end)
    end
    Player.CharacterAdded:Connect(onCharacterAdded) -- Connect the onCharacterAdded function to the Player.CharacterAdded Event
    if Player.Character then -- If a character is already spawned
        onCharacterAdded(Player.Character) -- Run the function for the character.
    end
end)
0
oh yes, i could have just created a characteradded function, thanks matiss112233 258 — 3y
0
no problem :) FunctionalMetatable 490 — 3y
Ad

Answer this question