Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my Player nil when I print it?

Asked by 5 years ago

Take a look - it says the player is nil

local Players = game:GetService("Players")
local player = Players.LocalPlayer

while true do
    wait(5)
    print(player)
    if owner.Value ~= nil then
        if owner.Value == player then
            print("yay")
        end     
    end
end

Thanks for the Help!

0
you are printing the object player not string player greatneil80 2647 — 5y

2 answers

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

The Player isn't nil, it's simply an Object, trying to print such a thing will give no results, simply because there is nothing to give; thus nil. You'll need a direct reference, such as a property or something of the sorts for it to give a viable output, for example, try printing their username:

print(Player.Name)

If this doesn't work out, and somehow the Player is truly nil try using this debug statement for varification or restarting ROBLOX Studio:

if (Player ~= nil) then print("accessible") else print("null") end
0
print(player) will print the player's name if the player object is valid, that's actually likely to be the source of confusion here. EmilyBendsSpace 1025 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

player is your Player object. When you call print(player), it prints the name of your player because the built-in print function tries to use tostring() on anything that's not already a string. For players, this returns the player name.

But... you can't compare a string that is a player name to the player object, that will not match. You have to do something like if stringValueObject.Value == player.Name then

Answer this question