How to detect if a player's character doesn't have something

Hey developers! I hope y’all doing very very well! Being direct I have the following question:

  1. What do you want to achieve?
    I want to detect if somebody’s character has an int value for example, but I don’t know exactly how to do it, so here I post the 2 options that I scripted:

  2. What is the issue?
    Not sure is the script is working.

  3. What solutions have you tried so far?

Option 1:

for i, v in pairs(game.Players:GetPlayers()) do 
  if game.Players[v].Character.IntValue == nil then
      --As the player doesn't have the "IntValue" on it's character and the script is supposed to work thendo something.
  end
end

Option 2:

for i, v in pairs(game.Players:GetPlayers()) do 
  if not game.Players[v].Character.IntValue then
      --As the player doesn't have the "IntValue" on it's character and the script is supposed to work thendo something.
  end
end

Thanks for your help and have a nice day and life ! goodbye! :grinning_face_with_smiling_eyes: :wave: :smiley:

Can I put “WaitForChild()” instead of “FindFirstChild()” ? Or that would be a scripting error?

Yes, that’s what I’m trying to do, absolutely as you say.

And so, if I want to randomly choose a player that doesn’t have the “IntValueN1” and the “IntValueN2” could I do it this way?

repeat  
ChosenPlayer = game.Players:GetPlayers()[math.random(1, #game.Players:GetPlayers())]
until
game.Players[ChosenPlayer].Character:FindFirstChild('IntValueN1').Value == nil and game.Players[ChosenPlayer].Character:FindFirstChild('IntValueN2') == nil 
					
print(PlayerName1.." is the randomly chosen player!")

Would this be well-written code? Or the “and” state inside the “until” would be wrong?
Thanks for your help

1 Like

I’m trying to check if this IntValue (not IntValue.Value) is literally not inside the player’s character:
image

You already have the player. In your condition you’re essential doing

game.Players[game.Players.Random_Player].Character...

You can simply use ChosenPlayer

ChosenPlayer.Character...

I’m also iffy about your checks. An IntValue's Value property will never be nil. If you’re checking whether the player’s character has those objects, remove the .Value bit so it’s not attempting to compare a number value to nil (in fact, if there wasn’t an IntValueN1, it would throw an exception stating that you’re attempting to index nil with Value).

until ChosenPlayer.Character:FindFirstChild("IntValueN1") == nil and ...


EDIT

A bit late but I just noticed this-
if not game.Players[v].Character.IntValue then
Although it was explained about how to check for an object, I want to point out how the Players service is being indexed for the player by using the player itself lol.
if not game.Players[game.Players.ThePlayer]...
Since v is already the player you can simply use that.
if not v.Character:FindFirstChild...

3 Likes