Hello developers, I am making a gamepass only area and I am checking to see if they own the gamepass, I have came across this error SOMETIMES, sometimes it errors, sometimes it works, here is my error!
“Workspace.MegaVIPRoom.Union.Script:12: attempt to index nil with ‘IsA’”
My code, and yes I know the gamepassId is set to xxxx
:GetPlayerFromCharacter function returns the Player from given Player.Character, in line 10, you’re indexing character.Parent, you don’t have to do .Parent, just keep it as :GetPlayerFromCharacter(character)
plr doesn’t exist, and it’s detecting a different part with no player (hence why plr does not exist). Instead, try verifying that character.Parent:FindFirstChild(“Humanoid”) is true, rather than checking if plr:IsA(“Player”) since GetPlayerFromCharacter always returns a player. (Note that the FindFirstChild method should be checked before getting the player from the character)
local plr = game.Players:GetPlayerFromCharacter(character.Parent)
local hum = character.Parent.Humanoid
if character.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(character.Parent)
if MarketPlaceService:UserOwnsGamePassAsync(plr.UserId, gamepassId) then
print("Owns the gamepass!")
else
print("Doesn't own the gamepass!")
hum.Health = 0
end
end
```
This works but it errors for the hats
Then you probably need to write an exception where if it is an accessory, look for the Parent above the actual Parent (since the hats are structured like (Character > Accessory > MeshPart) where body parts are (Character > MeshPart))
This error happens because you’re assuming every instance that touches the parent is a player. You should have no errors if you include the check to make sure plr isn’t nil, which would be replacing line 12 with: if plr then