Slite
Slite

Reputation: 27

Error: attempt to index nil with 'Agility'

Having problems with one script, I'm getting this error:

Workspace.AgilityZone.HitboxSide.Script:3: attempt to index nil with 'Agility' - Server - Script:3

script.Parent.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player.Agility.Value >= script.Parent.Parent.Requirement.Value then
        player.ZoneMulti.Value = script.Parent.Parent.Multi.Value
    end
end)

Upvotes: 2

Views: 111

Answers (1)

Kylaaa
Kylaaa

Reputation: 7204

The error is telling you that player is nil, and that's because game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) isn't guaranteed to return a player. All you need to do is add some safety checks.

script.Parent.Touched:Connect(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

    -- double check that we've got a player
    if not player then
        return
    end

    if player.Agility.Value >= script.Parent.Parent.Requirement.Value then
        player.ZoneMulti.Value = script.Parent.Parent.Multi.Value
    end
end)

Upvotes: 2

Related Questions