Dot indexing errors

I was wondering how dot Indexes work, I don’t want to have any errors in the console during a game because it just makes the game look unprofessional and could potentially reveal parts of anti-cheat systems to exploiters, so if I did it like this, even if I checked for the character in the return statement, it wouldn’t error before the return statement check right?

local character = player.Character
	if not character then
		return false
	end

Are you having error when indexing Character with player? Well, you could try this:

local character = player.Character or player.CharacterAdded:Wait()

if not character then
return false
end

2 Likes

If Player.Character is a property, or a property that points to an instance, then it shouldn’t error, but it is good practice to replace the first line in that block of code with local character = player.Character or player.CharacterAdded:Wait(), and delete if not character then return false end. In some cases you might need to connect to the character added event in the player and set the character variable to the new character every time a character is added. But if Player.Character is an object that you are trying to access, then it will error if Player.Character is nil.

Okay, well, I should have explained better but this is for a server-sided anticheat where I’m just doing some checks to make sure the player’s character and stuff is all in check

if not player:GetAttribute("Ping") then
		return false
	end
	if player:GetAttribute("Ping") > MAX_PING then
		return false
	end
	
	local character = player.Character
	if not character then
		return false
	end
	
	local humanoidRootPart = character.HumanoidRootPart
	if not humanoidRootPart then
		return false
	end
	
	local humanoid = character.Humanoid
	if not humanoid then
		return false
	end
	if humanoid.Health <= 0 then
		return false
	end

Basically, I was just wondering, it shouldn’t error because if player is a variable that I know will exist, then property will not error, but for example if I do like player.property.otherProperty, then it will error for otherProperty because it doesn’t know if property actually exists (before I check it)

Is this correct?

For example:

local object = Instance.new("Part") -- This variable we know for sure exists, will not error
local property = object.Property -- Should not error because we know "object" exists
local otherProperty = object.Property.OtherProperty -- Errors because we haven't checked if "property" actually exists

This is where I have gotten errors from using dot indexing, so I’m not sure if this is exactly how it works

You will get an error if the property doesn’t exist. But, the character property of player always exists, but it might not always store a reference to the character object. So, when it doesn’t hold a reference to the character object, it won’t error because this property does exist, but it’s value is nil.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.