Handling "__ is not a valid member of __" error

My game uses a teleport system type of thing. If a player dies and the server still tries to teleport them it gives that error. Here’s my code:

local workspacePlayer = workspace:FindFirstChild(v.Name)
		if workspacePlayer ~= nil and workspacePlayer.HumanoidRootPart then
		workspacePlayer.HumanoidRootPart.CFrame = CFrame.new(71, 27.5, -60)
	table.insert(playersPlaying,v.Name)
		end

The issue is I want to handle the error but I can’t handle it if it’s not a thing if that makes sense. I can’t use :waitforchild because it will wait until it can find it

What is not a valid member of what? Can’t really help if we’re not sure what the exact issue is! (unless it’s actually erroring “__” which I’ve never seen before).

Sorry,

HumanoidRootPart is not a valid member of Model “Workspace.{Player username}”

You’re trying to index the HumanoidRootPart here, which for some reason throws back an error

It is possible to avoid this from occurring, but might I ask what v.Name is supposed to be…? If you’re attempting to get Children from a specific instance, then that might be why cause whatever v is supposed to be, is unable to be found since not every Instance can return back with a part named HumanoidRootPart

local workspacePlayer = workspace:FindFirstChild(v.Name)
print(workspacePlayer)

if workspacePlayer ~= nil and workspacePlayer:FindFirstChild("HumanoidRootPart") then
    print("Found player")

	workspacePlayer.HumanoidRootPart.CFrame = CFrame.new(71, 27.5, -60)
    table.insert(playersPlaying, v.Name)
else
    print("Unable to find HumanoidRootPart / Unable to find object properly")
end
1 Like

Nevermind it dident work, same error

Best way to explain this is the game game is a color switcher. A random color is selected and dropped. If someone is on the block when it’s dropped if they were the last person it gives the error. the v.Name is gotten from for i,v in pairs(playerService:getPlayers)

I’m not experiencing the error when using your code but then again I’m not sure exactly what causes your character to die, so I can’t replicate precisely.

Are we allowed to send videos here? I can send a vid of the error

You sure can! Click this upload button to upload a video from your device files.

image

local function tpPlayers()
	local players = playerService:GetPlayers()
	for i,v in pairs(players) do
		local workspacePlayer = workspace:FindFirstChild(v.Name)
		if workspacePlayer and workspacePlayer.Humanoid.Health ~= 0 then
			wait(1)
		workspacePlayer.HumanoidRootPart.CFrame = CFrame.new(71, 27.5, -60)
	table.insert(playersPlaying,v.Name)
		end
	end
end

Show me the line where you call this function, perhaps I can see what’s causing it then.

I’m pretty sure this your character getting destroyed by the DestroyHeight of the world, you can either modify (It is located inside Workspace) it or get the Y Position of your Character

it is also best to check if the Player’s Character actually exists by doing:

if Player.Character then

Also, you dont need:

local workspacePlayer = workspace:FindFirstChild(v.Name)

just use Player.Character

Oh cool, also I am checking but when a player dies the character stays, it deletes some of the children though

No, The Character Basically get Deleted if you go to low
While yes, It does “exist”, The Item you are looking for is no longer there.

You can use FindFirstChild to properly prevent and handle these errors, that is how you should check for missing instances or instances you’re not sure will be there at all times. You do something like this with the player’s character already, it will be nil if it doesn’t exist, and you can do that where you are encountering your error too, for the player’s HumanoidRootPart.


Aside from that I also have some suggestions and info for you too that I hope you find helpful.

Generally if I were to write the code you wrote I’d write something like this (minus all the comments)

local character = player.Character
-- Then we can actually check for the character and the root part in the same line
local rootPart = character and character:FindFirstChild("HumanoidRootPart")
-- OR, with an if else expression (I usually like the latter for this stuff since it's short and still readable)
local rootPart = if character then character:FindFirstChild("HumanoidRootPart") else
-- We can check for the root part and then cancel if they don't have it
if not rootPart then return end

-- All the checks passed, so we are good to set their CFrame
rootPart.CFrame = CFrame.new(71, 27.5, -60)
table.insert(playersPlaying, player.Name) -- P.s. I might store the player themselves instead of the player's name

It is good to note by the way that nil values are falsy, so not nil is true, and it’ll act like false in conditions, and that can be nice if you want to keep your code simple.

The “if thing then return” pattern is called a guard clause I believe. I like to keep my guard clauses on one line given I am not returning anything at all (unlike most other things actually). I think it’s generally more conventional not to though, that is entirely just a preference of mine. Guard clauses can help a lot to avoid really big deep nested if statements and are nice to read, and easy to write. I often have tons of them in my code, and, it’s a really great pattern I think.

I would also suggest, rather than using FindFirstChild on the workspace, and using player names, it’s probably a lot cleaner to use player.Character, and it will be a bit more readable and a bit faster, and won’t let players with weird names (like Terrain) break your game. That’s something really good to keep in mind about using player names to find and keep track of stuff! (Player’s UserIds can often be a lot better, especially if you are storing data, because player names can change). If you already have the player (which I can assume you do judging by v.Name) you should totally use that instead. If the player doesn’t have a character you can check it easily since it’ll just be nil (and won’t throw an error since it’s a property of the player, rather than a child instance).

Generally, you would want to work with the Player instance instead of the player’s name, or getting their player by their character or character name, since you can get both from the player instance when you need them. Just some stuff to keep in mind, not that any of it is necessarily the “correct” way to do things (imo there isn’t a correct way to do anything, there are just ways with benefits and drawbacks).

2 Likes

I think it’s fixed. Thank you all!

1 Like

The character stays at first (for Players.RespawnTime seconds) but when the player respawns the old character is deleted and a new one is created. It’s not immediately obvious, but it’s actually an entirely different and brand new instance. (That can definitely matter if you use instances as table keys which is pretty common with some things, particularly when you want to associate stuff with a very specific instance)

1 Like

Gah Dam that’s a lot of word’s, Cool Explanation tho

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