2

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)
Nifim
  • 4,758
  • 2
  • 12
  • 31
Slite
  • 27
  • 5

1 Answers1

2

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)
Kylaaa
  • 6,349
  • 2
  • 16
  • 27
  • 2
    Another option is to prepend `player and` to the original if statement condition – Nifim Jun 20 '22 at 18:19
  • @Nifim, good suggestion, and that might be preferable if you want other stuff to happen when the object gets touched. – Kylaaa Jun 20 '22 at 20:31