Script doesn’t recognize values in the player

Hey, everyone!

I’ve recently had a problem with storing values in the player. Here’s what I’m doing:

 game.Players.PlayerAdded:Connect(function(player)
    local GameTag = Instance.new(“BoolValue”)
    GameTag.Name = “Gametag”
    GameTag.Parent = player
end)

for i, player in pairs(game.Players:GetPlayers()) do
    if player:FindFirstChild(“Gametag”) then
        print(“Player has a GameTag!”)
    end
end

Here’s the problem: I’ve looked in my player and it has the GameTag in it. However, nothing is being printed.

I also tried making a new game and testing this in that game and I got the same issue: nothing is printed. If i get rid of the if statement and write: “player:FindFirstChild(“Gametag”):destroy()” my output tells me that I’m trying to destroy a nil value even when I checked to see that I have the value in my player.

Thanks in advance :grinning: !

EDIT: I still haven’t received a solution, so if you think you have one, I’d really appreciate if you’d share. I’m lost and from what I know, this should work.

This is because you’re not using the correct quotation marks, not really sure which ones those are honestly. The compiler is forgiving in terms of which ones you can use, but that one is an exception.

Use the " ones and this should work.

That’s not the problem. The quotes I put in the post above are different from the ones in the actual script in studio. In studio, I used the quotes you were talking about.

Thanks anyway.

In that case, I could see why it doesn’t print. You iterate over all players in the game right after you create an listener for the .PlayerAdded event, so there is no guarantee that the player would have joined in the game and had that ValueObject made inside of them by the time that loop runs.

What are you trying to do exactly? If you need to access it on the client you could just use a WaitForChild statement.

You don’t have a player Parameter in the PlayerAdded event.

3 Likes

Sorry, that was just a typo in this version. The actual version has a parameter

I also tried doing this in a touched event with a part. Before stepping on the part, I made sure that I had the value in my player (which I did) then I stepped on the part and nothing printed. I’m completely confused as to why this isn’t working. I’ve looked at others’ code and we have the same code in which we just add a value then check if the player has the value.

To answer your question of what I’m trying to do: I’m using a value to see if the player is still in a minigame and not in the intermission lobby.

1 Like

If anyone else has this problem, I finally solved it! Basically, the player that stepped on the part was the character in the workspace, not the player in the player service, so there wasn’t a value in the workspace version of the player (AKA, the player that stepped on the part. I tested this theory out and it’s true. To solve it, I use an in pair loop to loop through all the players in the game through the player service and I use this code:

part.Touched:Connect(function(hit)
    for i, player in pairs(game.Players:GetPlayers()) do
         if hit.Parent.Name == player and player:FindFirstChildWhichIsA(“BoolValue”) then
             print(“It finally works!”)
         end
    end
end)

I don’t think that i need to do this, but this is the only thing that has worked after quite a bit of testing. Thanks to anyone who tried helping!

Just giving an easier solution, it can be done like this instead:

part.Touched:Connect(function(hit)
    local touchedPlayer = Players:GetPlayerFromCharacter(hit.Parent)

    if touchedPlayer then
         if touchedPlayer:FindFirstChildWhichIsA(“BoolValue”) then
             print(“It finally works!”)
         end
    end
end)
1 Like

thanks so much! I completely forgot I could use GetPlayerFromCharacter but I still knew that there was some easier way at the same time. Thanks a bunch!

1 Like