Is their a way I can stop a WaitForChild() from erroring?

Trying to make a chat command in a game Im making and I don’t want the WaitForChild to error if Im not in a server so

Is their a way I can stop a WaitForChild() from erroring?


You could add the timeOut parameter, and add this line: if child ~= nil then

I haven’t had any big problems with WaitForChild erroring, so I’m not sure if this will work.

1 Like

So like this?

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if player.Name == "NubblyFry" then
			if message == "/GetFlags1" then
				local Block = Instance.new("Part")
				Block.Size = Vector3.new(10,10,10)
				
				Block.Position = Instance.NubblyFry.HumanoidRootPart.Position
				
				Block.Parent = game.Workspace
				
				wait(0.5)
				
				Block:Destroy()
			end
		end
	end)
end)

and also, do you know why Block.Size = Vector3.new(10,10,10) isn’t working?

Instead, do:

Block.Position = player.Character.HumanoidRootPart.Position

I also recommend to use your UserId instead of your username, just in-case you change your username:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		if Players:GetUserIdFromNameAsync("NubblyFry") == player.UserId  then
			if message == "/GetFlags1" then
				local Block = Instance.new("Part")
				Block.Size = Vector3.new(10,10,10)
				
				Block.Position = player.Character.HumanoidRootPart.Position
				
				Block.Parent = game.Workspace
				
				wait(0.5)
				
				Block:Destroy()
			end
		end
	end)
end)
1 Like

It works now, thanks!

But can you like explain how the code works?

Which part do you want to be explained?

The GetUserId Async thing


Oh. It just finds a UserId of the player with the provided name.

Players:GetUserIdFromNameAsync("NubblyFry")

There are no problems here, I’m just explaining what this function does.

Thanks for the Help! I will use this nolage in the future >B)!

“WaitForChild()” only errors if the instance it is waiting for doesn’t exist (or doesn’t currently exist).

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Chatted:Connect(function(message)
			if player.Name == "NubblyFry" then
				if message == "/GetFlags1" then
					local Block = Instance.new("Part")
					Block.Size = Vector3.new(10,10,10)
					Block.Position = character.HumanoidRootPart.Position
					Block.Parent = workspace
					task.wait(0.5)
					Block:Destroy()
				end
			end
		end)
	end)
end)
1 Like

You may also want to change “.Position” for “.CFrame”, if you want to keep the orientation intact.