GetAttribute function returning nil

So I’m trying to print the lastAttacker, and winAward from the model, and for some reason its printing nil… Any suggestions? :slight_smile:

local CS = game:GetService("CollectionService")
local Enemies = CS:GetTagged("Enemy")
local ragdoll = require(game.ReplicatedStorage.Modules.Ragdoll)


local function EnemyDeath(Enemy : Model)
    print("Enemy died")
    local lastAttacker = Enemy:GetAttribute("LastAttacker")
    print(lastAttacker)
    local winAward = Enemy:GetAttribute("WinAward")
    print(winAward)

    Enemy.Humanoid.Died:Connect(function()
        if lastAttacker and lastAttacker:IsA("Player") then
            local playerName = lastAttacker.Name
            print("Last Attacker's Name:", playerName)
        end
    end)
end







for i,v in pairs(Enemies) do
    if v:FindFirstChild("Humanoid") ~= nil then
        print("Name: "..v:GetAttribute("Name"))
        print("Health: "..v:GetAttribute("Health"))

        local health = v:GetAttribute("Health")
        local name = v:GetAttribute("Name")
        
        v.Humanoid.MaxHealth = health
        v.Humanoid.Health = health
        v.Humanoid.BreakJointsOnDeath = false

        v.Humanoid.Died:Connect(function()
            --Ragdoll the NPC/Enemy
            if v:GetAttribute("isBoss") == true then
                --Boss logic
                EnemyDeath(v)
                return
            else
                --Maybe use a function here?
                EnemyDeath(v)
            end
        end)
    end
end

Your code doesn’t include where you are setting the attribute in the first place, so its hard to say.

I would do print(Enemy:GetFullName()) to verify if you are actually getting the enemy you are getting the attribute from, then verify in explorer if that enemy truly has the “LastAttacker” attribute.

Its being pre defined in the Models themselves, not through code.
image

Your attribute is called “lastAttacker” with a lowercase L. Your :GetAttribute call has an uppercase L.

1 Like

get attribute is case sensitive, Cody is correct.

I stg I feel dumb lol. Thanks!

1 Like

And thank you too! (30charasssss)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.