Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is my Player nil when I print it?

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!

1 comment

0

you are printing the object player not string player

2 answers

✓ Accepted answer

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

1 comment

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.